gpt4 book ai didi

c++ - 浏览字符串矩阵并将其存储在 vector 的 vector 中

转载 作者:行者123 更新时间:2023-11-28 01:28:57 26 4
gpt4 key购买 nike

我在一个字符串中有这个矩阵:

-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

我想浏览它并将其存储在矩阵 vector 或浮点矩阵中。我实际上可以将它存储在一个 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;
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 的 vector 中时,每行的最后一个数字 block 被删除,我不知道为什么。我要疯了,你能帮帮我吗?

这是我的代码:

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;
}

我想要最简单的解决方案来存储它,即使它在选项卡的 float 选项卡中也是如此。

最佳答案

通过使用标准的 C++ 操作和函数,您可以大大简化代码(并同时修复它):

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::vector<std::vector<float>> matrix;

std::string line;
while (std::getline(str, line))
{
std::istringstream iss(line);
matrix.emplace_back(std::istream_iterator<float>(iss), std::istream_iterator<float>());
}

参见例如here 的工作示例以及如何打印每个元素

关于c++ - 浏览字符串矩阵并将其存储在 vector 的 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com