Created
April 1, 2015 07:47
-
-
Save Tairy/fce4d0c16efd8355754b to your computer and use it in GitHub Desktop.
C++ vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
vector<int> Numvec; //定义一个整型的容器,它的类型是vector<int> | |
vector<int>::iterator Numer; //定义一个整型容器的迭代器,它的类型是vector<int>::iterator | |
int intNum; | |
while (cin>>intNum) | |
{ | |
Numvec.push_back(intNum); //往容器里面写数,用容器名.push_back(数据) | |
} | |
int *p=new int[Numvec.size()]; //定义一个整型的动态数组注意方式 左值是指针 右值是new | |
int *pNum=p; //将数据首地址赋给指针pNum指针 | |
for(Numer=Numvec.begin();Numer!=Numvec.end();Numer++,pNum++) | |
{ | |
*pNum=*Numer; //容器里面每个元素的值 就是: *迭代器名 | |
} | |
for (int i=0;i<5;i++) | |
{ | |
cout<<p[i]<<endl; | |
} | |
delete []p; //注意,动态创建的数据,就是new的东西,一定要删除,交还给内存 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment