1

从文本文件(Range.txt),

Range:1:2:3:4:5:6....:n:

有一个结果列表,但我只需要将数字 1 2 3 提取到最后一位,但有时最后一位可能会有所不同

所以我读入文件,提取分隔符并将其推入向量中。

ifstream myfile;
myfile.open("Range.txt");

istringstream range(temp);
getline(range, line,':');

test.push_back(line);

我如何捕捉所有价值?我有这个,它只捕获一个数字。

4

2 回答 2

2

我有这个,它只捕获一个数字

您需要使用循环:-

while (getline(range, line, ':')) 
  test.push_back(line);

稍后使用该向量,您可以对其进行处理以仅获取整数。

于 2013-08-10T16:43:44.263 回答
0

请阅读:Parsing and added string to vector

您只需更改分隔符(从whitespace:)。

std::ifstream infile(filename.c_str());
std::string line;

if (infile.is_open())
{
    std::cout << "Well done! File opened successfully." << std::endl;
    while (std::getline(infile, line, ':'))
    {
        std::istringstream iss(line);
        std::vector<std::string> tokens{std::istream_iterator<std::string>(iss),std::istream_iterator<std::string>()};

        // Now, tokens vector stores all data.
        // There is an item for each value read from the current line.
    }
}
于 2013-08-10T16:36:31.670 回答