从cin中读取双矩阵
Reading double matrix from cin
我需要从cin中读取小队矩阵,但我不知道这个矩阵的大小。所以我需要读取第一行(用空格或制表符分隔的双数字,直到行尾)。在解析这一行以获得双数的计数之后。若行中有n个双数,则矩阵大小为nxn。我该怎么做?
代码:
unsigned int tempSize = 0;
double tempPoint;
double * tempArray = new double [0];
string ts;
getline(std::cin, ts);
std::istringstream s(ts);
while (s >> tempPoint){
if (!s.good()){
return 1;
}
tempArray = new double [tempSize+1];
tempArray[tempSize] = tempPoint;
tempSize++;
}
cout << "temp size " << tempSize << endl;
输出:
temp size 0
Program ended with exit code: 0
-
使用
std::getline
读取一行。 -
从线路上构造一个
std::istringstream
。 -
继续读取
std::istringstream
中的数字,并将它们添加到std::vector
中。当您阅读完行的内容后,std::vector
中的项目数将为您提供矩阵的排名。 -
使用用于读取矩阵第一行的相同逻辑来读取矩阵的其他行。
编辑
void readRow(std::vector<int>& row)
{
std::string ts;
std::getline(std::cin, ts);
if ( std::cin )
{
std::istringstream s(ts);
int item;
while (s >> item){
row.push_back(item);
}
}
std::cout << "size " << numbers.size() << std::endl;
}
相关文章:
- 从 cin 读取不会返回整个输入字符串
- 使用 cin 读取空行
- 为什么从 std::cin 读取输入后右括号字符的值有时不是")"?
- 使用 cin.get 读取字符数组
- C++ 读取未知数量的整数到 cin 并输出总和
- std::cin 在读取 EOF 并清除后不再读取
- 为什么我的 cout 一次执行,尽管 cin 应该在两者之间读取?
- 如何使用 cin 将整数从控制台读取到向量中
- 如何从 cin 读取直到输入非数字内容?
- 如何从文件中读取,然后继续从cin读取?
- 使用 cin.get() 读取字符
- 无法获取哨兵值以读取多个 CIN 输入?
- 如何使用std::cin函数后修复从文件读取的问题
- 当读取错误类型时,CIN覆盖我的初始化值
- 如何在CIN中以C 的CIN中的所有读取为字符串
- C++ cin 将字符串读取到 int 类型中返回 0
- 使用 cin 读取大型输入时程序冻结
- 读取cin直到EOF
- c++访问器mutator未读取cin-switch case语句
- c++中不能完全读取cin输入