如何使用迭代器填充未知大小的向量?

时间:2019-12-16 11:09:39

标签: c++ visual-c++ iterator

我正在尝试创建一个我不知道其大小的图形,用户填充矢量直到用户想要为止。 以及如何使用迭代器获取元素?

我未完成的代码:

#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) 
        }
}

请帮助我完成它。

1 个答案:

答案 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 ++中,它就是()