我在字符串中有这个矩阵:
-78.45 5120 45.369 7.456
-0.140 1.012 1.161 7.456
-4.4287 8.963 1.121 7898
-8.753 8.159 3.852 2.415
0.000 2.456 4.655 6.041
0.000 3.132 8.275 1.788
7.489 8.056 7.288 5.698
4.050 7.456 7.340 2.025
0.090 9.478 9.395 6.416
1.132 6.866 8.450 2.126
6.222 5.142 7.596 0.56
9.121 2.256 5.641 3.741
8.896 1.488 2.858 2.456
我想浏览并将其存储在矩阵向量或float矩阵中。 我实际上可以将其存储在单个向量中,这是功能代码:
int main()
{
std::istringstream str(
"-78.45 5120 45.369 7.456\n"
"-0.140 1.012 1.161 7.456\n"
"-4.4287 8.963 1.121 7898\n"
"-8.753 8.159 3.852 2.415\n"
"0.000 2.456 4.655 6.041\n"
"0.000 3.132 8.275 1.788\n"
"7.489 8.056 7.288 5.698\n"
"4.050 7.456 7.340 2.025\n"
"0.090 9.478 9.395 6.416\n"
"1.132 6.866 8.450 2.126\n"
"6.222 5.142 7.596 0.56\n"
"9.121 2.256 5.641 3.741\n"
"8.896 1.488 2.858 2.456\n");
std::string space = " ";
std::string line;
size_t l_position = 0;
std::vector<float> vectorNumber;
while (std::getline(str, line)) {
while ((l_position = line.find(space)) != std::string::npos)
{
float nombre = std::stof(line.substr(0, l_position));
vectorNumber.push_back(nombre);
line.erase(0, l_position + space.length());
}
vectorNumber.push_back(std::stof(line));
}
int w = 0;
for(vector<float>::iterator it=vectorNumber.begin(); it!=vectorNumber.end(); ++it)
{
cout << *it << " ";
w++;
if (w==4){
cout << endl;
w = 0;
}
}
return 0;
}
当我尝试将其存储在vector向量中时,每行数字的最后一块被删除,我不知道为什么。我快疯了,你能帮我吗?
这是我的代码:
int main()
{
std::istringstream str(
"-78.45 5120 45.369 7.456\n"
"-0.140 1.012 1.161 7.456\n"
"-4.4287 8.963 1.121 7898\n"
"-8.753 8.159 3.852 2.415\n"
"0.000 2.456 4.655 6.041\n"
"0.000 3.132 8.275 1.788\n"
"7.489 8.056 7.288 5.698\n"
"4.050 7.456 7.340 2.025\n"
"0.090 9.478 9.395 6.416\n"
"1.132 6.866 8.450 2.126\n"
"6.222 5.142 7.596 0.56\n"
"9.121 2.256 5.641 3.741\n"
"8.896 1.488 2.858 2.456\n");
std::string space = " ";
std::string line;
size_t l_position = 0;
std::vector<float> vectorNumber;
std::vector<vector<float>> vectorOfVectorNumber;
while (std::getline(str, line)) {
while ((l_position = line.find(space)) != std::string::npos)
{
float nombre = std::stof(line.substr(0, l_position));
vectorNumber.push_back(nombre);
line.erase(0, l_position + space.length());
}
vectorOfVectorNumber.push_back(vectorNumber);
}
int w = 0;
for (int i = 0; i < vectorOfVectorNumber.size(); i++)
{
for (int j = 0; j < vectorOfVectorNumber[i].size(); j++)
{
cout << vectorOfVectorNumber[i][j] << " ";
}
cout << endl;
}
return 0;
}
我想要最简单的解决方案来存储它,即使它位于tab的浮动标签中。
答案 0 :(得分:2)
通过使用标准的C ++操作和函数,可以大大简化代码(并同时修复):
class MyClass {
private myArray: (string | null)[] = [];
myFunc = (str: string) => str.toUpperCase();
myOtherFunc = () => {
this.myArray
.filter(str => str !== null)
.map(this.myFunc); // Type 'string | null' is not assignable to type 'string'.
}
}
例如参见here提供了一个可行的示例以及如何打印每个元素
答案 1 :(得分:0)
问题是您的第二个循环while ((l_position = line.find(space)) != std::string::npos)
跳过了最后一个条目,因为它找不到空格。要解决此问题,您可以使用另一个字符串流。您还需要将vector<float>
放在内部while循环之前,整个过程看起来像这样:
while (std::getline(str, line)) {
stringstream ss(line);
string number = "";
std::vector<float> vectorNumber;
while (std::getline(ss,number,' '))
{
vectorNumber.push_back(stof(number));
}
vectorOfVectorNumber.push_back(vectorNumber);
}