stl_vector review

#include <iostream>
#include <vector>
#include <algorithm> //for_each
#include <ctime>
using namespace std;

void STRConstructor () // initialization, input
{
Vector <int> VEC (5,12);
// VEC COUT << << endl; Error

vector<int> vec1(5);
for(int i=0;i<5;i++)
cout << vec1[i] << endl;

vector <int> vec3 (vec) ; // vector <char> can not <> must be the same intermediate
// cout << vec3 << endl; Error

vector<int>::iterator ite=vec3.begin();
vector<int>::iterator ite1=vec3.end();
vector<int> vec4(ite,ite1);
//cout << vec4 << endl; 错误
for(int i=0;i<3;i++)
cout << vec4[i] << endl;
}

void strcapacity()
{
vector<int>vec(3);
vector<int>vec2();
//cout << vec.empty() << '\n' << vec2.empty() << endl; //报错?vec2
vec.reserve(10);
cout << vec.size() << vec.capacity() << endl;
vec.resize(2); //重新设置元素个数
cout << vec.size() << vec.capacity() << endl;

Vector // <int> vec1 (. 5);
//vec1.push_back(1);
//vec1.push_back(1); // different compilers, different effects of existing capacity vs half
// cout << vec1. capacity () << endl;


// re-apply for space will fail iterator 0
}

void fun (int i) // not template
{
COUT << << I '';
}

void output() //输出
{
vector<int>vec;
for(int i=0;i<10;i++)
{
vec.push_back(i);
cout << vec[i] << vec.at(i) <<endl;
}
cout << vec.back() << endl;
vector<int>::iterator ite=vec.begin();
for(;ite!=vec.end();ite++)
{
cout << *ite << endl;
}
for_each(vec.begin(),vec.end(),fun);
}

void operation () // manipulation functions
{
Vector <int> VEC;
Vector <int> vec1 (5, 5);
for (int I = 0; I <10; I ++)
{
vec.push_back (I); // End Add
}
//vec.insert(vec.begin()+3,12);
//vec.insert(vec.begin()+2,3,3);
//vec.insert(vec.begin()+ 3, vec1.begin (), vec1.begin ( ) + 2);

vec.pop_back (); // Last deleted

//vec.erase(vec.begin()+3);

//vec.erase(vec.begin()+3,vec.end());

//vec.swap(vec1,vec);

cout << (vec<vec1) << endl;

for_each(vec.begin(),vec.end(),fun);
}

void algorithm() //需要头文件include<algorithm>
{
vector<int>vec;
vector<int>vec1(5,5);
for(int i=0;i<10;i++)
{
vec.push_back(10-i); //尾添加
}
for_each(vec.begin(),vec.end(),fun);

sort (vec.begin (), vec.end ());

for_each(vec.begin(),vec.end(),fun);

sort(vec.begin(),vec.end(),greater<int>()); //include<functional>

for_each(vec.begin(),vec.end(),fun);
}

void supplement() //补充
{
vector<int>vec;
for(int i=0;i<10;i++)
{
vec.push_back(i);
}

vector<int>vec1(8,14);

//vec.assign(vec1.begin (), vec1.end ()); // clear the previous, the new assignment

//vec.assign(3,0); // clear the previous, the new assignment

//vec.clear();

srand ((unsigned int) time (0)); // true random

random_shuffle (vec.begin (), vec.end ()); // randomly scrambled

for_each(vec.begin(),vec.end(),fun);

cout << endl;

random_shuffle (vec.begin (), vec.end ());

for_each(vec.begin(),vec.end(),fun);

cout << endl;
}

int main()
{
vector<int> vec;
//STRConstructor();
//strcapacity();
//output();
//operation();
//algorithm();
//supplement();
return 0;
}

/ *
Vector conventional fixed spatial
Array
a valarray cmath.h - manipulation functions in the C language, and calculation of relevant

* /
/ *
Vector can put other objects string structure pointer container objects


array

valarray // There are many mathematical functions he has packaged inside a cmath.h function
* /

Guess you like

Origin www.cnblogs.com/sos3210/p/12292341.html