vector的一点使用
来源:互联网 发布:马云淘宝如何盈利 编辑:程序博客网 时间:2024/04/28 06:37
这里主要是尝试使用了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
我相信,只要努力学习,总会有收获的。
- vector的一点使用
- vector的一点知识
- Vector容器的一点说明
- 关于vector的一点源码分析
- Vector和ArrayList的一点区别
- 关于vector删除操作的一点提示。
- STL中的vector容器的一点总结
- c++中vector的一点介绍
- STL中的vector容器的一点总结
- STL中的vector容器的一点总结
- 关于vector::size 异常的一点经验
- Vector容器的一点个人收获
- STL中的vector容器的一点总结
- STL中的vector容器的一点总结
- vector的使用
- Vector 的使用
- Vector类的使用
- vector 的使用
- vc经验
- Adsense的配色技巧
- [转贴]逐步设置vim C/C++语法高亮显示和自动缩进
- 数学与哲学(张景中院士)
- 从追MM谈Java的23种设计模式(转)
- vector的一点使用
- 两个日志记录程序操作同一个log文件
- 不用window.open();实现窗口没有工具栏
- 身体器官工作表
- 基本概念esm
- 俄罗斯邮政将迁移到Linux 有关机构已开始测试Linux
- asp.net中页面间传值方法
- lib文件格式分析,以及从lib文件提取obj的思路和源码
- Ubuntu 8.04与XP SP3性能大比拼