vector操作

来源:互联网 发布:网络发帖推广 编辑:程序博客网 时间:2024/05/01 09:02

1、一次性打印vector中的所有元素

2、将两个vector元素组合到一个vector中

3、利用函数 max_element,min_element,distance可以获取Vector中最大、最小值的值以及位置索引

</pre><p></p><pre name="code" class="cpp">#include <vector>   #include <iterator>   #include <algorithm> #include <iostream>using namespace std;int main(){vector<float> A;A.push_back(1.0);A.push_back(2.0);A.push_back(3.0);vector<float>B;B.push_back(4.0);B.push_back(5.0);B.push_back(6.0);cout<<"***A******"<<endl;copy (A.begin(), A.end(), ostream_iterator<float> (cout, "\n"));// 打印A的元素cout<<"***B******"<<endl;copy (B.begin(), B.end(), ostream_iterator<float> (cout, "\n")); cout<<"****new A******"<<endl;      A.insert(A.end(),B.begin(),B.end());//合并B到A     copy (A.begin(), A.end(), ostream_iterator<float> (cout, "\n"));     vector<double>::iterator maxValue = max_element(begin(A),end(A));      cout << "Max element is " << *maxValue<< " at position " <<distance(begin(A),maxValue)<<endl;        auto minValue = std::min_element(begin(A), std::end(A));     cout << "min element is " << *minValue<< " at position " <<distance(begin(A),maxValue)<<endl;return 0;}

0 0
原创粉丝点击