タイトルが示すように、stdin から整数の文字列を読み込んでいます。読み込もうとしているデータは、テキスト ファイルに次の形式で表示されます。
3
4
7 8 3
7 9 2
8 9 1
0 1 28
etc...
最初の 2 行は常に 1 つの数字 (問題ありません!) で、次の行にはそれぞれ 3 つの数字があります。このファイルは stdin として私のプログラムにリダイレクトされます (myprogram < textfile)。
私は非常に多くのことを試しましたが、これをうまく行うことができませんでした! とても単純に思えますが、整数に変換する場所 (または方法) につまずきます。これが私の最新の試みです:
int main()
{
string str, str1, str2;
int numCities;
int numRoads, x, y, z;
cin >> numCities >> numRoads;
cout << numCities << " " << numRoads << endl;
//getline(cin, str1);
while( getline(cin, str))
{
char *cstr;
cstr = new char[str.size()+1];
strcpy(cstr, str.c_str());
x = atoi(strtok(cstr, " ")); //these will be stored in an array or something
y = atoi(strtok(NULL, " ")); //but right now i just want to at least properly
z = atoi(strtok(NULL, " ")); //store the appropriate values in these variables!
}
return 0;
}
atoi を使おうとすると segfault します...
前もって感謝します!