如何用vector定义string类型的指针变量

ActorKeyy 2012-10-09 01:25:04
我正在学习C++ PRIMER,第五章有个5.18题目,说要定义一个vector对象,每个元素都是指向string类型的指针
我用了以下两种方式,VC++ 6.0都提示错误了:
#include <vector>
using std::vector;
......
string s1("I am Aka");
1/ vector<string *> sp(&s1);
2/ vector<sting> *sp(&s1);
求大侠指教。
...全文
399 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
用了楼上的方案,编译不出错了
但是,
cout << *iter << endl;
这个打印出来怎么是个地址呢?这里我不是解引用了吗?
[/Quote]
iter本身是指针的泛化,这里相当于string**
cout << *(*iter) << endl;
ActorKeyy 2012-10-09
  • 打赏
  • 举报
回复
对哦,我忘记是指针了。。。。应该+2个*的。。
汗,我太粗心了。


[Quote=引用 8 楼 的回复:]
C/C++ code


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string *> svec;

string s1 = "hello ";
string s2 = "c++!";

……
[/Quote]
ActorKeyy 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
vs2008 output:
hello c++!

楼主如果学习STL的话,最好换用vc2008或者更新的版本
[/Quote]

我是听别人说 XP系统(我32位的机器)用VC6更稳定,所以才用这个的。
不知道,我这里是否适合用VS2010?这个VS2010的破解版又能到哪里下载呢?
ActorKeyy 2012-10-09
  • 打赏
  • 举报
回复
用了楼上的方案,编译不出错了
但是,
cout << *iter << endl;
这个打印出来怎么是个地址呢?这里我不是解引用了吗?
northcan 2012-10-09
  • 打赏
  • 举报
回复
vs2008 output:
hello c++!

楼主如果学习STL的话,最好换用vc2008或者更新的版本
northcan 2012-10-09
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string *> svec;

string s1 = "hello ";
string s2 = "c++!";

svec.push_back(&s1);
svec.push_back(&s2);

vector<string *>::iterator itr;
for (itr = svec.begin(); itr != svec.end(); ++itr)
cout << **itr;

return 0;
}
mujiok2003 2012-10-09
  • 打赏
  • 举报
回复
error C2653: 'vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,class std::allocator<class std::basic_string<char,struct std::ch
ar_traits<char>,class std::allocator<char> > *> >' : is not a class or namespace name


编译器不认识vector
#include <vector>
#include <string>
using namespace std;

string s1("I am Aka");
vector<string *> sp(1, &s1);// 一个&s1

ActorKeyy 2012-10-09
  • 打赏
  • 举报
回复
Compiling...
5_18.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(17) : error C2653: 'vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,class std::allocator<class std::basic_string<char,struct std::ch
ar_traits<char>,class std::allocator<char> > *> >' : is not a class or namespace name
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(17) : error C2065: 'iterator' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(17) : error C2146: syntax error : missing ';' before identifier 'iter'
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(17) : error C2065: 'iter' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(17) : error C2440: '=' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > ** ' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(18) : error C2446: '!=' : no conversion from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > ** ' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(18) : error C2040: '!=' : 'int' differs in levels of indirection from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > ** '
C:\Program Files\Microsoft Visual Studio\MyProjects\chap05\5_18.cpp(20) : error C2100: illegal indirection
执行 cl.exe 时出错.

chap05.exe - 1 error(s), 0 warning(s)
ActorKeyy 2012-10-09
  • 打赏
  • 举报
回复
这样也还是错了。。。。我头晕了。。。。

#include <iostream>
#include <string>
#include <vector>
using std::vector;
using std::string;
using std::cout;
using std::endl;
int main()
{
string s1("I am aka!");
string s2("i am aka.");
string s3("I AM AKA");
vector<string *> sp;
sp.push_back (&s1);
sp.push_back (&s2);
sp.push_back (&s3);
vector<string *>::iterator iter = sp.begin();
while ( iter != sp.end())
{
cout << *iter << endl;
++iter;
}
return 0;
}
baichi4141 2012-10-09
  • 打赏
  • 举报
回复
VS6.0对于模板甚至C++标准都有支持不完善之处,换GCC或者更高版本的VS
一般使用模板——特别是标准库——时<和>要注意两边留个空格,否则很容易凑成<<和>>操作符
2/的string写成了sting,而且该语句定义的是一个指向vector的指针,不能这么初始化
此外,string也是std命名空间里的,你using了吗
nickwu1220 2012-10-09
  • 打赏
  • 举报
回复
写错鸟
vector<sting*> sp;
sp.push_back(&s1);
nickwu1220 2012-10-09
  • 打赏
  • 举报
回复
vector<sting*> sp;
sp.push_back(s1);
悲伤的肉包子 2012-10-09
  • 打赏
  • 举报
回复
string s1("I am Aka");
vector<string *> vec;
vec.push_back(&s1);

64,681

社区成员

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

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