c++ - 如何使用迭代器填充未知大小的 vector ?

标签 c++ visual-c++ iterator

我正在尝试创建一个我不知道其大小的图形,用户填充 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/

相关文章:

c++ - 在 VC6 dll 中使用 auto_ptr 导致崩溃

python - 使用Python的itertools生成二维表索引

c++ - 无法从具有虚拟继承的类派生(C++ 虚拟继承)

c++ - 为什么 std::is_rvalue_reference 没有按照广告宣传的那样做?

c++ - 核心延迟测试 ARMv8.1

c++ - QtConcurrent Map 不能使用类方法

c++ - MSVC 直接构造函数调用扩展

c++ - 枚举数组作为参数 c++

c++ - 错误 : request for member ‘end’ in . ... 属于非类类型 ‘double’

c++ - Typedef vector 迭代器