vector的一点使用
2008-05-27 10:30
246 查看
这里主要是尝试使用了vector的一些基本方法以及将vector作为vector的模版来使用的两种情况,程序也很简单。如下:
(因为使用哪个“插入代码”功能实在等不下去了,就直接粘贴了)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> v;
for (int i = 0; i<5 ;i++ )
{
v.push_back(i);
}
// copy the vector to the screen, from first to last.
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
// get the top element of th vector
int i=v.back();
cout<<i<<endl;
// // get the bottom element of th vector
i=v.front();
cout<<i<<endl;
// copy the vector to the screen from last ro first
copy(v.rbegin(),v.rend(),ostream_iterator<int>(cout," "));
cout<<endl;
// get the element at specific place
cout<<v.at(2)<<endl;
return 0;
}
该程序运行结果如下:
0 1 2 3 4
4
0
4 3 2 1 0
2
另外一个使用vector作为vector的模版的程序:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<vector<int>> vv;
vector<int> vi;
vi.push_back(4);
vi.push_back(6);
vv.push_back(vi);
vector<int> vi1;
vi1.push_back(1);
vi1.push_back(2);
vv.push_back(vi1);
int c = 0;
// get the vector's capacity. If using size(), it should be as follows:
// int s = vv.size();
// while (c<s) {...}
// note: "while(c<vv.size) {...}" won't get the last element. After pop_back(), the size() decreases.
while (c<vv.capacity())
{
/* first, get the top element via back(), copy its elements to screen;
* then pop up it to make the top the next element.
*/
vector<int> v = vv.back();
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
vv.pop_back();
c++;
}
return 0;
}
该程序结果如下:
1 2
4 6
我相信,只要努力学习,总会有收获的。
(因为使用哪个“插入代码”功能实在等不下去了,就直接粘贴了)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> v;
for (int i = 0; i<5 ;i++ )
{
v.push_back(i);
}
// copy the vector to the screen, from first to last.
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
// get the top element of th vector
int i=v.back();
cout<<i<<endl;
// // get the bottom element of th vector
i=v.front();
cout<<i<<endl;
// copy the vector to the screen from last ro first
copy(v.rbegin(),v.rend(),ostream_iterator<int>(cout," "));
cout<<endl;
// get the element at specific place
cout<<v.at(2)<<endl;
return 0;
}
该程序运行结果如下:
0 1 2 3 4
4
0
4 3 2 1 0
2
另外一个使用vector作为vector的模版的程序:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<vector<int>> vv;
vector<int> vi;
vi.push_back(4);
vi.push_back(6);
vv.push_back(vi);
vector<int> vi1;
vi1.push_back(1);
vi1.push_back(2);
vv.push_back(vi1);
int c = 0;
// get the vector's capacity. If using size(), it should be as follows:
// int s = vv.size();
// while (c<s) {...}
// note: "while(c<vv.size) {...}" won't get the last element. After pop_back(), the size() decreases.
while (c<vv.capacity())
{
/* first, get the top element via back(), copy its elements to screen;
* then pop up it to make the top the next element.
*/
vector<int> v = vv.back();
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
vv.pop_back();
c++;
}
return 0;
}
该程序结果如下:
1 2
4 6
我相信,只要努力学习,总会有收获的。
相关文章推荐
- React Native react-native-vector-icons 使用小记
- 使用django框架的connection对象需要注意的一点
- C++ Vector 使用心得
- 关于layoutSubviews的一点使用总结
- Thrust快速入门教程(二)——Vector的使用
- 对《Ext2.0 form使用实例》的一点补充
- c++ vector数组对象的使用
- STL_稀疏图,树_使用vector邻接表存储
- C#中使用ref 和 out 的一点认识
- 在Linux中使用STL迭代器的一点注意事项
- STL系列之vector的常见功能使用
- 使用vector创建二维动态数组,并使用sort对其进行排序
- Vector、ArrayList、List使用深入剖析
- ofstream 使用的一点主意事项
- [C++] std::vector 使用
- vector使用
- 关于使用YTKNetwork的一点小笔记
- 加载纹理与使用glGenTextures时应注意的一点(解决吃内存)[转]
- c++中使用基类指针Vector 存放子类对象(0xcccccccc 处未处理的异常: 0xC0000005: 读取位置 0xcccccccc 时发生访问冲突 )
- Java中Vector、ArrayList、List使用深入剖析