如何从文件中为C ++中的向量向量赋值?

时间:2018-06-11 04:41:40

标签: c++

我已声明:

  elasticsearch {
    hosts => ["localhost:9200"]
    index => "schedule1_documents"
    document_id => "%{id}"              <-- add this line with the proper ID field
  }

我正在阅读文件:

const std::vector<std::vector<uint8_t>> input;
std::string s = "";

当我这样做时,

std::ifstream file("some.txt");

显示错误 int count = 0; while (std::getline(file, s)) { input[count] = somefunction(s); //return value is of this function is of type: vector<uint8_t> count += 1; }

1 个答案:

答案 0 :(得分:-2)

将您的代码更改为:

 int count = 0;
    while (std::getline(file, s))
    {
        input.push_back(somefunction(s)); //return value is of this function is of type: vector<uint8_t>
        count += 1; // not mandatory you can use size() function to get the count;
    }

向量无法初始化为数组(如array[i]=7),vect[i]=9 不正确(如果未调整大小或无内存分配),尽管可以将其初始化为宣言中的数组 矢量可以像数组一样访问(cout<<vect[i]);
有关初始化矢量的更多详细信息,请查看以下链接:
https://www.geeksforgeeks.org/initialize-a-vector-in-cpp-different-ways/