我正在尝试创建一个我不知道其大小的图形,用户填充 vector 直到用户需要为止。 以及如何使用迭代器获取元素?
我的代码还没有完成:
#include <iostream>
#include <vector>
#include <iterator>
#include <list>
void main(void)
{
{
using namespace std;
vector<vector<char>> graph;
vector<vector<char>>::iterator outerMove;
vector<char>::iterator innerMover;
cout << "enter your verteces name one by one and write done when you are done";
for (outerMove = graph.begin(); outerMove != graph.end(); ++outerMove)
{
//first get the size of vector , how much user wants enters
}
for (innerMover = )
{
//now here graph.push_back(innerMove)
}
}
请帮我完成它。
最佳答案
在这种情况下你不使用迭代器,你使用push_back
并让 vector 完成它的工作(即自动调整大小):
vector<std::string> graph;
std::string outermove; // a proper "list of chars"!
while ((cin >> outermove) && outermove != "done")
graph.push_back(outermove);
与问题无关:
void main()
在C++中是非法的,main
需要返回int
。 (void)
是一种编写空参数列表的 C 方法 - 在 C++ 中它只是 ()
。
关于c++ - 如何使用迭代器填充未知大小的 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59355435/