将选项卡分隔的值从文本文件读入数组 我对C++很陌生,我面临一个问题:

将选项卡分隔的值从文本文件读入数组 我对C++很陌生,我面临一个问题:,c++,arrays,read-text,C++,Arrays,Read Text,我有一个文本文件,如下所示: 500 1120 10 number1,1 number1,2 ... number1,500 number2,1 . . number1120,1 #include <iostream> #include <sstream> // Assume input is 12,34,56. You can use // getline or something to read a line from // input f

我有一个文本文件,如下所示:

500

1120

10

number1,1 number1,2   ...   number1,500

number2,1

.

.

number1120,1
#include <iostream>
#include <sstream>

// Assume input is 12,34,56. You can use 
// getline or something to read a line from
// input file. 
std::string input = "12,34,56";

// Now convert the input line which is string
//  to string stream. String stream is stream of 
// string just like cin and cout. 
std::istringstream ss(input);
std::string token;

// Now read from stream with "," as 
// delimiter and store text in token named variable.
while(std::getline(ss, token, ',')) {
    std::cout << token << '\n';
}

因此,文本文件顶部的前两个值描述了矩阵的维度。现在我想写一个代码,它将矩阵中的所有文件读入
int
值的数组或向量。我可以读取前三个值(500,1120,10),并使用<代码> GETLION>代码>和<代码> StrugSuth<代码>,但我无法理解如何用循环分离矩阵抽头。

< P>可以考虑用循环逐行读取矩阵,并使用记录器对当前行进行拆分。(例如,
std::strok
)或在分隔符处拆分行的嵌套循环。
有一个线程。

可以考虑用循环逐行读取矩阵,并使用标记器(如<代码> STD::Strutok < /代码>)分割当前行或在分隔符上分割行的嵌套循环。 有一条关于的线索。

类似这样的东西:

500

1120

10

number1,1 number1,2   ...   number1,500

number2,1

.

.

number1120,1
#include <iostream>
#include <sstream>

// Assume input is 12,34,56. You can use 
// getline or something to read a line from
// input file. 
std::string input = "12,34,56";

// Now convert the input line which is string
//  to string stream. String stream is stream of 
// string just like cin and cout. 
std::istringstream ss(input);
std::string token;

// Now read from stream with "," as 
// delimiter and store text in token named variable.
while(std::getline(ss, token, ',')) {
    std::cout << token << '\n';
}
#包括
#包括
//假设输入为12,34,56。您可以使用
//获取行或从中读取行的内容
//输入文件。
std::string input=“12,34,56”;
//现在转换为字符串的输入行
//字符串流。字符串流是
//就像cin和cout一样。
std::istringstream ss(输入);
字符串标记;
//现在用“,”as从流中读取
//分隔符并将文本存储在名为variable的标记中。
while(std::getline(ss,token,,')){
std::cout类似这样的内容:

500

1120

10

number1,1 number1,2   ...   number1,500

number2,1

.

.

number1120,1
#include <iostream>
#include <sstream>

// Assume input is 12,34,56. You can use 
// getline or something to read a line from
// input file. 
std::string input = "12,34,56";

// Now convert the input line which is string
//  to string stream. String stream is stream of 
// string just like cin and cout. 
std::istringstream ss(input);
std::string token;

// Now read from stream with "," as 
// delimiter and store text in token named variable.
while(std::getline(ss, token, ',')) {
    std::cout << token << '\n';
}
#包括
#包括
//假设输入为12,34,56。您可以使用
//获取行或从中读取行的内容
//输入文件。
std::string input=“12,34,56”;
//现在转换为字符串的输入行
//字符串流。字符串流是
//就像cin和cout一样。
std::istringstream ss(输入);
字符串标记;
//现在用“,”as从流中读取
//分隔符并将文本存储在名为variable的标记中。
while(std::getline(ss,token,,')){

std::cout一点解释会激励我对你的答案进行投票;)+OP的值是分开制表符的(但这是一个细节)。我在代码中添加了注释。如果你有任何具体问题,请随时问我。一点解释会激励我对你的答案进行投票;)+OP的值是分开制表符的(但这是一个细节)。我在代码中添加了注释。如果您有任何具体问题,请随时问我。您应该提供一个小示例,说明您迄今为止为解决问题所做的工作。此处对此进行了说明。您应该提供一个小示例,说明您迄今为止为解决问题所做的工作。此处进行了说明