我正在尝试创建一个我不知道其大小的图形,用户填充矢量直到用户想要为止。 以及如何使用迭代器获取元素?
我未完成的代码:
#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)
}
}
请帮助我完成它。
答案 0 :(得分:2)
在这种情况下,您不使用迭代器,而使用push_back
并让向量完成其工作(即自动调整大小):
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 ++中,它就是()
。