这是我的错误代码:
error: 'class __gnu_debug_def::vector, std::allocator >, std::allocator, std::allocator > > >::std' has not been declared compilation terminated due to -Wfatal-errors.
此代码只是我为产生相同错误而编写的通用代码,但我的原始代码试图执行 strings::vector::at(i)
操作:
#include <iostream>
int main(){
std::vector<std::string> strings;
std::string string;
std::stringstream range1d;
range1d.str( "Hello0,Hello1,Hello2,Hello3,Hello4,Hello5");
while(std::getline(range1d,string,',')) {
strings.push_back(string);
}
for(std::vector<std::string>::const_iterator i = strings.begin(); i != strings.end() ; ++i) {
cout << "at: " << strings.std::vector::at(i) << endl ;
}
return 0;
}
我是 C++ 的新手,虽然给出了错误代码,但我真的不知道如何解决这个问题。用谷歌搜索了一段时间。我发现了一些关于声明 typename 之类的东西,但我无法应用它,所以它才有意义。 感谢您的帮助
最佳答案
i
是一个迭代器,为了获取值,您需要做的就是取消引用它。
std::cout << "at: " << *i << std::endl;
at
成员函数采用索引。即,一个整数,指示您想要的元素在 vector 中的位置。您可以在如下所示的循环中使用它:
for (unsigned int i = 0; i<strings.size(); ++i)
{
std::cout << "at: " << strings.at(i) << std::endl;
}
此外,您需要为您使用的所有设施包含适当的 header ,即 <vector>
, <sstream>
, 和 <string>
.您还需要符合条件 cout
和 endl
在std
命名空间。
关于c++ - 尝试访问 vector<string> 元素时出现错误,文本中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18237248/