您的位置:首页 > 其它

vector操作

2015-06-08 18:07 295 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: