You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取TXT文件并为每行创建独立的string类型vector?

Hey Luis, let's break this down step by step to solve your problem. First, we'll handle reading each line of your text file into its own single-element vector<string>, then we'll cover how to merge those vectors pairwise until only one remains.

Solution for Reading File Lines into Individual Vectors

Step 1: Read the File and Generate Single-Element Vectors

In C++, you can use std::ifstream to read the file line by line, and for each line, create a new vector<string> that only contains that line. We'll store all these small vectors in a larger vector<vector<string>> to keep track of them.

Here's a complete code example:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main() {
    // This will hold all our single-line vectors
    std::vector<std::vector<std::string>> line_vectors;
    std::ifstream input_file("your_text_file.txt");

    // Always check if the file opened successfully
    if (!input_file.is_open()) {
        std::cerr << "Error: Could not open the file!" << std::endl;
        return 1;
    }

    std::string current_line;
    // Read each line until we reach the end of the file
    while (std::getline(input_file, current_line)) {
        // Create a new vector containing just the current line
        std::vector<std::string> single_line_vec;
        single_line_vec.push_back(current_line);
        // Add this small vector to our collection
        line_vectors.push_back(single_line_vec);
    }

    input_file.close();

    // Optional: Verify the result by printing each vector's content
    for (size_t i = 0; i < line_vectors.size(); ++i) {
        std::cout << "Vector " << i + 1 << ": " << line_vectors[i][0] << std::endl;
    }

    // Proceed to merge step here...
    return 0;
}

How this works:

  • We open the file with std::ifstream and validate the open operation to avoid errors.
  • std::getline reads each line of the file into current_line (this handles lines with spaces too).
  • For each line, we create a new vector<string>, add the line to it, then store that vector in line_vectors. If your file has 25 lines, line_vectors will have 25 elements, each a vector with exactly one string.

Step 2: Pairwise Merge Until One Vector Remains

Once you have your collection of single-line vectors, you can repeatedly merge adjacent pairs until only one vector (containing all lines) is left. Here's how to implement that:

Add this code right after the file reading section (before return 0):

// Merge vectors pairwise until only one remains
while (line_vectors.size() > 1) {
    std::vector<std::vector<std::string>> merged_temp;

    // Iterate over the vectors two at a time
    for (size_t i = 0; i < line_vectors.size(); i += 2) {
        std::vector<std::string> combined;
        // Merge the current vector into combined
        combined.insert(combined.end(), line_vectors[i].begin(), line_vectors[i].end());
        
        // If there's a next vector, merge that too (handles odd counts)
        if (i + 1 < line_vectors.size()) {
            combined.insert(combined.end(), line_vectors[i+1].begin(), line_vectors[i+1].end());
        }

        merged_temp.push_back(combined);
    }

    // Replace our original collection with the merged one for the next iteration
    line_vectors = std::move(merged_temp);
}

// The final merged vector is line_vectors[0]
std::cout << "\nFinal merged vector contains " << line_vectors[0].size() << " lines." << std::endl;

How this works:

  • We use a while loop that runs as long as there's more than one vector in our collection.
  • In each iteration, we create a temporary collection to hold merged vectors.
  • We loop through the original vectors in steps of 2: merging vector i with i+1 (if it exists) into a new combined vector.
  • After processing all pairs, we replace the original collection with the temporary merged one.
  • When the loop ends, line_vectors[0] will be the single vector containing all lines from the original file.

内容的提问来源于stack exchange,提问作者Luis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:15:43