65,210
社区成员
发帖
与我相关
我的任务
分享
//定义一个vector对象,其每个元素都是指向string类型的指针
//读取该vector对象,输出每个string的内容及其长度
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string*> spvec;
string str;
cout<<"Enter some strings(Ctrl+Z to end)"<<endl;
while(cin>>str)
{
string *pstr=new string;
*pstr=str;
spvec.push_back(pstr);
}
//输出每个string的内容及其长度
vector<string*>::iterator it=spvec.begin();
while(it!=spvec.end())
{
cout<<**it<<" "<<(**it).size()<<endl;
++it;
}
cout<<sizeof spvec<<endl;
it=spvec.begin();
while(it!=spvec.end()){
delete *it;
++it;
}
system("PAUSE");
return 0;
}
//定义一个vector对象,其每个元素都是指向string类型的指针
//读取该vector对象,输出每个string的内容及其长度
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> svec;
string str;
cout<<"Enter some strings(Ctrl+Z to end)"<<endl;
while(cin>>str)
{
svec.push_back(str);
}
//输出每个string的内容及其长度
vector<string>::iterator it=svec.begin();
while(it!=svec.end())
{
cout<<*it<<" "<<(*it).size()<<endl;
++it;
}
cout<<sizeof svec<<endl;
system("PAUSE");
return 0;
}