STL中问题(问题就是程序注释部分)

哈哈王戈多 2011-07-08 01:02:06
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <time.h>
#include <list>
#include <numeric>
#include <functional>
using namespace std;

#define SIZE 10
vector<int> vint(SIZE);
void main()
{
for(int i=0;i<10;i++)
vint.push_back(i+1);
copy(vint.begin(),vint.end(),ostream_iterator<int>(cout,"\t"));

}
问题描述:程序输出结果为:
0 0 0 0 0 0 0 0 0 0 //请问是怎么来的??不明白!
1 2 3 4 5 6 7 8 9 10
...全文
58 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfk 2011-07-08
  • 打赏
  • 举报
回复
vector<int> vint(SIZE); // 构造函数,初始化为SIZE个元素
LZ可以:
cout << vint.size() << endl;
for(int i=0;i<10;i++)
vint.push_back(i+1);
cout << vint.size();

看下size

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
Constructs a vector container object, initializing its contents depending on the constructor version used:

explicit vector ( const Allocator& = Allocator() );
Default constructor: constructs an empty vector, with no content and a size of zero.
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
Repetitive sequence constructor: Initializes the vector with its content set to a repetition, n times, of copies of value.
template <class InputIterator> vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
Iteration constructor: Iterates between first and last, setting a copy of each of the sequence of elements as the content of the container.
vector ( const vector<T,Allocator>& x );
Copy constructor: The vector is initialized to have the same contents (copies) and properties as vector x.

更多vector,参考:
http://blog.csdn.net/yfkiss/article/details/6537234
luciferisnotsatan 2011-07-08
  • 打赏
  • 举报
回复
vector<int> vint(SIZE);
初始化大小为10
vint.push_back(i+1);
这个push_back是在10个初始化的元素后继续追加,结果有20个元素了。
hahayezhe112 2011-07-08
  • 打赏
  • 举报
回复
vector<int> vint(SIZE);

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧