利用迭代器输出 vector对象的大小问题
原程序如下#include<iostream>#include<string>#include<vector>/*使用说明*/usingstd::cout;usingst...
原程序如下
#include <iostream>
#include <string>
#include <vector>
/*使用说明*/
using std::cout;
using std::string;using std::cin;using std::endl;
using std::vector;
/*主函数*/
int main()
{
//string array[3]={"Hello","World","YangBo"};
//vector<string> Test (array,array+3); //此方法运用数组来实现Test容器的初始化
vector <string> Test;
string words;
cin>>words;
Test.push_back(words);
cin>>words;
Test.push_back(words);
cin>>words;
Test.push_back(words);
auto it=Test.begin(); //定义Test的迭代器it
cout<<"The size of Test is:"<<Test.size()<<endl;
cout<<"The size of Test is:"<<it->size()<<endl;
cout<<"The size of Test is:"<<(*it).size()<<endl;
}
但是输出的大小有差别,这是为什么啊? 展开
#include <iostream>
#include <string>
#include <vector>
/*使用说明*/
using std::cout;
using std::string;using std::cin;using std::endl;
using std::vector;
/*主函数*/
int main()
{
//string array[3]={"Hello","World","YangBo"};
//vector<string> Test (array,array+3); //此方法运用数组来实现Test容器的初始化
vector <string> Test;
string words;
cin>>words;
Test.push_back(words);
cin>>words;
Test.push_back(words);
cin>>words;
Test.push_back(words);
auto it=Test.begin(); //定义Test的迭代器it
cout<<"The size of Test is:"<<Test.size()<<endl;
cout<<"The size of Test is:"<<it->size()<<endl;
cout<<"The size of Test is:"<<(*it).size()<<endl;
}
但是输出的大小有差别,这是为什么啊? 展开
1个回答
2015-04-28
展开全部
Test.size() 跟 Test[0].size() 当然不是同一个东西……
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询