在下面的代码中有一个错误,我希望在每个子向量中打印第一个元素:
vector<vector<int>> logs{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
for (auto beg = logs.begin(); beg != logs.end(); beg++) {
cout << *beg[0] << endl;
}
错误来自cout << *beg[0]...
Indirection requires pointer operand ('std::vector<int, std::allocator<int>>' invalid)
所以我的问题是:迭代器的引用应该是向量"logs“中的一个子向量,所以我使用下标来访问它的第一个元素。为什么有一个std::vector<int, std::allocator<int>>
对象?std::allocator<int>
从何而来?如何访问子向量中的元素?
https://stackoverflow.com/questions/71996867
复制相似问题