我想访问另一个 vector 中第一个条目内的 vector 第一个条目中的字符串。我的代码:
typedef std::vector<std::string> DatabaseRow;
std::vector<DatabaseRow*> data;
//getting data from database
//(dont need this for this example)
//then print first entry out
printf("%s.\n",dbresult.data[0][0].c_str());
但是我得到了错误:
error: ‘class std::vector, std::allocator >, std::allocator, std::allocator > > >’ has no member named ‘c_str’
有什么见解吗?
最佳答案
您正在存储指向数据库的指针:
std::vector<DatabaseRow*> data;
^ Pointer
所以你必须像这样访问它:
(*dbresult.data[0])[0].c_str()
1. (*dbresult.data[0]) // Dereference the pointer
2. [0] // Access the internal Vector
3. .c_str() // Use the string
或者,做更好的事情,不要把它变成一个指针,按照它应该的方式将它用作一个实际的对象:
std::vector<DatabaseRow> data;
关于C++ 在 vector 中访问 vector 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50531800/