我在尝试访问vector <string>元素时遇到错误,文本中的信息

tru*_*gen 0 c++ string vector

这是我的错误代码:

错误:'class __gnu_debug_def :: vector,std :: allocator>,std :: allocator,std :: allocator >>> :: std'由于-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;
}  
Run Code Online (Sandbox Code Playgroud)

我是C++的新手,虽然给出了错误代码,但我真的不知道如何解决这个问题.谷歌搜索了一段时间.我发现了一些关于声明typename或者其他东西的东西,但我无法应用它以使它有意义.感谢您的任何帮助

Ben*_*ley 5

i 是一个迭代器,为了获得该值,您需要的只是取消引用它.

std::cout << "at: " << *i << std::endl;
Run Code Online (Sandbox Code Playgroud)

所述at构件函数采用的指标.也就是说,一个整数表示所需元素所在的向量中的位置.您可以在看起来像这样的循环中使用它:

for (unsigned int i = 0; i<strings.size(); ++i)
{
    std::cout << "at: " << strings.at(i) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

此外,还需要包括相应的头文件为你使用,即设施<vector>,<sstream><string>.您还需要资格cout,并endlstd命名空间.