C++ 浏览字符串矩阵并将其存储在向量的向量中

C++ 浏览字符串矩阵并将其存储在向量的向量中,c++,string,parsing,matrix,vector,C++,String,Parsing,Matrix,Vector,我有一个字符串中的矩阵: -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

我有一个字符串中的矩阵:

-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
我想浏览它并将其存储在矩阵向量或浮点矩阵中。 我可以将它存储在一个向量中,这是函数代码:

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;
}
intmain()
{
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.0909.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.8961.4882.8582.456”;
std::string space=“”;
std::字符串行;
大小和位置=0;
向量向量数;
while(std::getline(str,line)){
while((l_position=line.find(space))!=std::string::npos)
{
float nombre=std::stof(行substr(0,l_位置));
矢量编号。推回(nombre);
line.erase(0,l_位置+空格.length());
}
矢量号。推回(标准::stof(线));
}
int w=0;
对于(vector::iterator it=vectorNumber.begin();it!=vectorNumber.end();++it)
{

CUT

你可以通过使用标准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>());
}
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.0909.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.8961.4882.8582.456”;
向量矩阵;
std::字符串行;
while(std::getline(str,line))
{
标准::istringstream iss(线);
matrix.emplace_back(std::istream_迭代器(iss),std::istream_迭代器());
}

<例如,对于一个工作示例,以及如何打印每个元素

,可以通过使用标准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>());
}
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.0909.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.8961.4882.8582.456”;
向量矩阵;
std::字符串行;
while(std::getline(str,line))
{
标准::istringstream iss(线);
matrix.emplace_back(std::istream_迭代器(iss),std::istream_迭代器());
}

例如,请参阅工作示例以及如何打印每个元素问题在于((l_position=line.find(space))!=std::string::npos)时的第二个循环
这会跳过最后一个条目,因为它找不到空格。要解决此问题,可以使用另一个stringstream。您还需要将
向量带到内部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);
}
while(std::getline(str,line)){ 弦流ss(线); 字符串编号=”; 向量向量数; while(std::getline(ss,number.)) { 矢量编号。推回(stof(编号)); } vectorOfVectorNumber.向后推(vectorNumber); }
问题在于你的第二个循环
while((l_position=line.find(space))!=std::string::npos)
这跳过了最后一个条目,因为它找不到空格。要解决这个问题,你可以使用另一个stringstream。你还需要将你的
向量带到内部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);
}
while(std::getline(str,line)){ 弦流ss(线); 字符串编号=”; 向量向量数; while(std::getline(ss,number.)) { 矢量编号。推回(stof(编号)); } vectorOfVectorNumber.向后推(vectorNumber); }
你错过了最后一个,因为你在搜索一个空格,但行尾没有空格,只有
\n
。我建议你不要手动创建一个stringstream,然后从中读取数字。请注意,平面向量不仅更容易读取,而且实际上是最有效的无论如何,存储矩阵的一种有效方法(只要矩阵是稠密的。对于备用矩阵,其他表示法更好),因此可能值得考虑继续使用工作代码。您错过了最后一个代码,因为您搜索了一个空格,但行尾没有空格,只有
\n
。我建议您不要手动执行此操作,而是从行字符串创建一个stringstream并从中读取数字。请注意,不仅仅是平面向量更容易读入,它实际上是存储矩阵的最有效的方法(只要矩阵是密集的。其他表示形式更适合备用矩阵),因此可能值得考虑继续使用工作代码。