/*exercise9.11
Show an example of each of the six ways to create and initialize a vector.Explain what value each vector contains.
*/
[C++] 纯文本查看 复制代码 #include<iostream>
#include<vector>
void Example(const std::vector<int>&v,int index);
int main()
{
using std::vector;
vector<int> v1; //Don't have any element
vector<int> v2={11,22,33,44,55 }; //Five elements :11 22 33 44 55
vector<int> v3=v2; //Same as v2
vector<int> v4(10,1); //Ten elements,each initialized to 1
vector<int> v5(10); //Ten elements,each initialized to 0
vector<int> v6(v3.cbegin(),v3.cend());//Same as v3
Example(v1,1);
Example(v2,2);
Example(v3,3);
Example(v4,4);
Example(v5,5);
Example(v6,6);
}
void Example(const std::vector<int>&v,int index)
{
std::cout<<"Vector"<<index<<" element:";
for(auto p:v)
std::cout<<p<<" ";
std::cout<<std::endl;
}
|