C++ 在 vector 中访问 vector 中的字符串

标签 c++ vector

我想访问另一个 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/

相关文章:

c++ - 使用 boost :odeint 的简单 C++ 程序中的断言错误

c++ - 从 C++ 中的 .csv 文件中读取 double

c++ - 调用名称为类名本身的函数?

c++ - 如何在程序退出到重启之间及时监控windows中的某个目录?

c++ - 来自 STL 的 vector 与列表 - 删除方法

c++ - 如果没有找到 map 元素会返回什么?

java - LibGDX Vector3 Lerp 问题

c++ - 将多个 vector 转换为数组,或返回它们 (c++)

c++ - 您应该在头文件中包含源代码吗?

c++ - 如何在 VS 2013 中使用 QT 4.8.x?