C++在向量中的向量中访问字符串

A. *_*ete 1 c++ 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());
Run Code Online (Sandbox Code Playgroud)

但后来我得到错误:

错误:'class std :: vector,std :: allocator>,std :: allocator,std :: allocator >>>'没有名为'c_str'的成员

有什么见解吗?

Fan*_*Fox 6

您正在存储指向数据库的指针:

std::vector<DatabaseRow*> data;
                       ^ Pointer
Run Code Online (Sandbox Code Playgroud)

所以你必须像以下一样访问它:

(*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
Run Code Online (Sandbox Code Playgroud)

或者,做得更好,不要使它成为指针,将其用作实际对象的方式:

 std::vector<DatabaseRow> data;
Run Code Online (Sandbox Code Playgroud)