【问题标题】:How to split a string into two integers over several lines C++如何在多行C ++中将字符串拆分为两个整数
【发布时间】:2014-03-24 01:56:16
【问题描述】:

我一直在尝试从文本文件中检索保存的数据。存储的数据都是数字,用~分隔。我已经设法让它打印出其中一行(顶行),但是我一直无法弄清楚如何处理整个文件。

每行只有两个数字(整数),另一个向量的 X 和 Y 位置。这个想法是将每个整数分配给向量中的相应变量。我没能走到那一步,因为我无法让它超过第 1 行。但我认为通过数组大小为 2,并且数组临时存储值,将其分配给向量,然后用下一个可以工作的值覆盖它。但又一次没能走到那一步。

以下是我一直在尝试使用的代码;

........
string loadZombieData;
loadFile >> loadZombieData; //Data gets read from the file and placed in the string

vector<string>   result; //Stores result of each split value as a string


stringstream  data(loadZombieData);
string line;
while(getline(data,line,'~'))
{
    result.push_back(line); 
}

for(int i = 0; i < result.size(); i++){
    cout << result[i] << "  ";
}
.......

澄清一下,这不是我的代码,这是我在 Stackoverflow 上找到的一些代码,所以我还不能完全确定它是如何工作的。正如我所说,我一直试图让它读取多行,然后使用 for 循环将根据需要将结果分配给其他向量变量。任何帮助表示赞赏:)

【问题讨论】:

  • 读了 3 遍还是不太清楚你想要什么...你能举一个你加载的文件的例子和预期的输出吗?
  • 对不起。该文件是这样的:10~8\n4~13\n8~2 等(对的数量取决于保存了多少僵尸(其他向量之一)。程序当前保存 X 和 Y 位置(x~y)以及我的m 试图做的是检索值并将这些值应用于向量的 x 和 y 位置(基本上重新加载以前的位置)。如果有帮助?:/

标签: c++ vector split


【解决方案1】:

使用两个while循环:

std::vector<std::string> result;
std::vector<int> numbers;

std::string filename;
std::ifstream ifile(filename.c_str());

if (!ifile.is_open()) {
    std::cerr << "Input file not opened! Something went wrong!" << std::endl;
    exit(0);
}

std::string temp;

//loop over the file using newlines as your delimiter
while (std::getline(ifile, temp, '\n')) {
   //now temp has the information of each line.
   //create a stringstream initialized with this information:

   std::istringstream iss(temp);//this contains the information of ONE line

   //now loop over the string stream object as you would have in your code sample:
   while(getline(iss, temp,'~'))
   {
       //at this point temp is the value of a token, but it is a string
       result.push_back(temp); //note: this only stores the TOKENS as strings

       //so to store the token as a int or float, you need to convert it to that
       //via another stringstream:

       std::istringstream ss(temp);

       //if your number type is float, change it here as well as in the vector
       //initialization of `numbers`:
       int num = 0;
       //this checks the stream to ensure that conversion occurred.
       //if it did, store the number, otherwise, handle the error (quit - but, this is up to you)
       //if stringstreams aren't your cup of tea, try some others (refer to this link):
       //http://stackoverflow.com/questions/21807658/check-if-the-input-is-a-number-or-string-c/21807705#21807705
       if (!(ss >> num).fail()) {
           numbers.push_back(num);
       }
       else {
           std::cerr << "There was a problem converting the string to an integer!" << std::endl;
       }
    }
}

注意:此版本逐字存储数字:即不知道一行中有多少数字。但是,这是可以调和的,因为您所要做的就是每行输出 n 个数字。在您的情况下,您知道每 2 个数字将代表一行中的数字。

这需要:

#include <string>
#include <vector>
#include <cstdlib>
#include <sstream>

【讨论】:

  • 您好,感谢您的帮助。这可能很愚蠢,但我似乎无法打开“ifile”。它只是恢复到错误然后关闭。我已经尝试定义filename 字符串以及文件名,保持原样,使用fstream 和loadZombieData 变量定义它,它们都不起作用。
  • @NeoKuro:你把文件名放在filename里了吗?
  • 我试过了,我觉得哈哈。我试过filename = "saveZombieData.txt" 我试过filename = loadZombieData 以及filename = loadFile。他们都没有工作,他们都吐出一些错误(即如果ifile没有打开,程序会关闭程序)
  • @NeoKuro:嗯。你的数据文件在哪里?如果您的文件名是:“saveZombieData.txt”,它应该与编译文件位于同一目录中
  • 我现在觉得很傻。我刚刚意识到该文件实际上称为 saveZombieFile.txt 而不是数据哈哈。已编辑:我修复了另一个问题,即 for 循环的次要放置问题。现在似乎可以工作了,谢谢大家。现在只需要弄清楚如何将~ 左侧的数字分配给 x 并将其右侧的数字分配给 y :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 2015-03-07
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
相关资源
最近更新 更多