将包含特殊字符的字符串数组转换为 int 数组。输入来自文件

发布于 2025-01-10 17:48:51 字数 2239 浏览 4 评论 0原文

我这里的问题是获取文件中的所有整数,并将其存储到 int 数组中(当然没有 | ),然后用它做一些事情(这里我只需要帮助来打印数组)。

文件中的数据据说是一个 10x10 矩阵。

当我尝试使用 std::stringstream 时,我的代码输出是另一个带有垃圾值的 10x10。

输入:

1|11|2|13|2|2|2|11|13|2
1|11|2|13|2|2|2|13|2|11
1|13|1|13|2|2|2|2|2|2
1|13|1|12|2|2|2|2|2|2
1|13|1|13|1|2|2|2|2|2
1|13|1|13|1|1|2|2|2|2
1|13|1|13|1|1|1|2|2|2
1|13|1|13|1|1|1|1|2|2
1|13|1|13|1|1|1|1|1|2 
1|13|1|13|1|1|1|1|1|1

代码:

#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
using namespace std;

int main(){
    //read data from file and store in a 2d array
    ifstream myfile("input.txt");
    string arr[10][20];
    for(int i=0;i<10;i++){
        for(int j=0;j<20;j++){
            getline(myfile,arr[i][j],'|');
        }
    }
    //convert string to int
    //use stringstream
    int arr2[10][20];
    for(int i=0;i<10;i++){
        for(int j=0;j<20;j++){
            stringstream ss;
            ss<<arr[i][j];
            ss>>arr2[i][j];
        }
    }
    //print arr2
    for(int i=0;i<10;i++){
        for(int j=0;j<20;j++){
            cout<<arr2[i][j]<<" ";
        }
        cout<<endl;
    }
    myfile.close();
    return 0; 
}

输出应该是:

1 11 2 13 2 2 2 11 13 2
1 11 2 13 2 2 2 13 2 11
1 13 1 13 2 2 2 2 2 2
1 13 1 12 2 2 2 2 2 2
1 13 1 13 1 2 2 2 2 2
1 13 1 13 1 1 2 2 2 2
1 13 1 13 1 1 1 2 2 2
1 13 1 13 1 1 1 1 2 2
1 13 1 13 1 1 1 1 1 2 
1 13 1 13 1 1 1 1 1 1   

我的输出:

1 11 2 13 2 2 2 11 13 2 
1 13 2 2 2 2 2 2 13 1 
1 2 2 2 2 2 13 1 13 1 
1 2 2 2 13 1 13 1 1 1 
1 2 13 1 13 1 1 1 1 1 
1994842136 12 7086696 7085352 1994774642 0 1994842144 1994771436 0 0 
6416848 2004213955 7086772 6416972 12 7086696 0 4 48 1994842112 
2004213726 1 7149280 7149288 7086696 0 1988425164 0 2003400672 12 
1999860416 6416972 2 1999966512 1999657456 1999691632 1999846272 1999845632 1999819744 1999860464 
4194304 1994719472 0 6417024 0 6418312 2004471984 775517426 -2 6417120 

My problems here is to take all the integers in a file, and store to an int array (of course without |), and then do something with it (here I just need help to print out the array).

The data from the file is said to be a 10x10 matrix.

My code output is another 10x10 with trash values, as I tried using std::stringstream.

Input:

1|11|2|13|2|2|2|11|13|2
1|11|2|13|2|2|2|13|2|11
1|13|1|13|2|2|2|2|2|2
1|13|1|12|2|2|2|2|2|2
1|13|1|13|1|2|2|2|2|2
1|13|1|13|1|1|2|2|2|2
1|13|1|13|1|1|1|2|2|2
1|13|1|13|1|1|1|1|2|2
1|13|1|13|1|1|1|1|1|2 
1|13|1|13|1|1|1|1|1|1

Code:

#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
using namespace std;

int main(){
    //read data from file and store in a 2d array
    ifstream myfile("input.txt");
    string arr[10][20];
    for(int i=0;i<10;i++){
        for(int j=0;j<20;j++){
            getline(myfile,arr[i][j],'|');
        }
    }
    //convert string to int
    //use stringstream
    int arr2[10][20];
    for(int i=0;i<10;i++){
        for(int j=0;j<20;j++){
            stringstream ss;
            ss<<arr[i][j];
            ss>>arr2[i][j];
        }
    }
    //print arr2
    for(int i=0;i<10;i++){
        for(int j=0;j<20;j++){
            cout<<arr2[i][j]<<" ";
        }
        cout<<endl;
    }
    myfile.close();
    return 0; 
}

The output should be:

1 11 2 13 2 2 2 11 13 2
1 11 2 13 2 2 2 13 2 11
1 13 1 13 2 2 2 2 2 2
1 13 1 12 2 2 2 2 2 2
1 13 1 13 1 2 2 2 2 2
1 13 1 13 1 1 2 2 2 2
1 13 1 13 1 1 1 2 2 2
1 13 1 13 1 1 1 1 2 2
1 13 1 13 1 1 1 1 1 2 
1 13 1 13 1 1 1 1 1 1   

My output:

1 11 2 13 2 2 2 11 13 2 
1 13 2 2 2 2 2 2 13 1 
1 2 2 2 2 2 13 1 13 1 
1 2 2 2 13 1 13 1 1 1 
1 2 13 1 13 1 1 1 1 1 
1994842136 12 7086696 7085352 1994774642 0 1994842144 1994771436 0 0 
6416848 2004213955 7086772 6416972 12 7086696 0 4 48 1994842112 
2004213726 1 7149280 7149288 7086696 0 1988425164 0 2003400672 12 
1999860416 6416972 2 1999966512 1999657456 1999691632 1999846272 1999845632 1999819744 1999860464 
4194304 1994719472 0 6417024 0 6418312 2004471984 775517426 -2 6417120 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2025-01-17 17:48:51

两个问题:当文件中只有 10x10 时,您尝试读取 10x20。此外,您的代码假设所有相邻数字之间都有 | ,但事实并非如此。一行中的最后一个数字与下一行中的第一个数字之间没有 |

相应地更改大小并在 | 处分割它们之前阅读整行:

string arr[10][10];
for(int i=0;i<10;i++){
    std::string line;
    getline(myfile,line);
    std::stringstream linestream{line};
    for(int j=0;j<10;j++){
        getline(linestream,arr[i][j],'|');
    }
}

现场演示

这是对代码的最小更改。接下来,我建议直接从流中提取整数,而不是首先提取字符串,然后将它们转换为整数(通过将字符串放入另一个流中,然后提取整数,您可以使用 ifstream 来完成此操作>)。

Two issues: You try to read 10x20 when there is only 10x10 in the file. Further your code assumes that there is a | between all adjacent numbers, but thats not the case. There is no | between the last number in a line and the first in the next line.

Change the size accordingly and read full lines before you split them at |:

string arr[10][10];
for(int i=0;i<10;i++){
    std::string line;
    getline(myfile,line);
    std::stringstream linestream{line};
    for(int j=0;j<10;j++){
        getline(linestream,arr[i][j],'|');
    }
}

Live Demo

This is minimum changes on your code. Next I would suggest to extract integers from the stream directly instead of first extracting string and then convert them to integers (by placing the strings into another stream and then extracting the integer, you could do this already with the ifstream).

雨轻弹 2025-01-17 17:48:51

基本的问题是输入文件的内容无法很好地映射到您尝试对该内容执行的操作。特别是,您假设输入文件中的每个字符都由 | 字符分隔,但事实并非如此,因为任何特定行中的最后一个字符与中的第一个字符之间都有换行符下一行。

另请注意,您创建了一个具有 10 行和 20 列的二维数组,但您的输入文件只有 10 行和 >10 列。

更好的方法是使用 2D std::vector ,如下所示:


#include <iostream>
#include<sstream>
#include<string>
#include<vector>
#include <fstream>

int main()
{
    std::ifstream inputFile("input.txt");
    std::string line;
    
    //create a 2D vector 
    std::vector<std::vector<int>> arr;
    
    int num = 0; //this will hold the current integer value from the file 
    std::string numTemp;
    if(inputFile)
    {
        while(std::getline(inputFile, line)) //read line by line 
        {
            std::vector<int> temp;//create a 1D vector 
            std::istringstream ss(line);
            
            while(std::getline(ss, numTemp, '|') && (std::istringstream(numTemp) >> num)) //read integer by integer 
            {
                //emplace_back the current number num to the temp vector 
                temp.emplace_back(num);
            }
            
            //emplace_back the current 1D vector temp to the 2D vector arr
            arr.emplace_back(temp);
        }
    }
    else 
    {
        std::cout<<"input file cannot be opened"<<std::endl;
    }
    //lets confirm if our 2D vector contains all the elements correctly
    for(const auto& row: arr)
    {
        for(const auto& col: row)
        {
            std::cout<<col<<" ";
        }
        std::cout<<std::endl;
    }
    return 0;
}

上述程序的输出可以看到 此处

与内置数组相比,使用 std::vector 的优点是:

  1. 您不必知道 input.txt 文件中有多少行和列预先。
  2. 不必所有行都具有相同的列数。

也就是说,使用 std::vector 是灵活的。

The basic problem is that the content of your input file doesn't map well to the operation that you're trying to do on that content. In particular, you've assumed that every character in your input file is separated by a | character which is not the case since there is a newline between the last character in any particular line and the first character in the next line.

Also, do note that you have created a 2D array that has 10 rows and 20 columns but your input file has only 10 rows and 10 columns.

Better would be to use a 2D std::vector as shown below:


#include <iostream>
#include<sstream>
#include<string>
#include<vector>
#include <fstream>

int main()
{
    std::ifstream inputFile("input.txt");
    std::string line;
    
    //create a 2D vector 
    std::vector<std::vector<int>> arr;
    
    int num = 0; //this will hold the current integer value from the file 
    std::string numTemp;
    if(inputFile)
    {
        while(std::getline(inputFile, line)) //read line by line 
        {
            std::vector<int> temp;//create a 1D vector 
            std::istringstream ss(line);
            
            while(std::getline(ss, numTemp, '|') && (std::istringstream(numTemp) >> num)) //read integer by integer 
            {
                //emplace_back the current number num to the temp vector 
                temp.emplace_back(num);
            }
            
            //emplace_back the current 1D vector temp to the 2D vector arr
            arr.emplace_back(temp);
        }
    }
    else 
    {
        std::cout<<"input file cannot be opened"<<std::endl;
    }
    //lets confirm if our 2D vector contains all the elements correctly
    for(const auto& row: arr)
    {
        for(const auto& col: row)
        {
            std::cout<<col<<" ";
        }
        std::cout<<std::endl;
    }
    return 0;
}

The output of the above program can be seen here.

The advantage of using std::vector over built in array is that :

  1. You don't have to know the how many rows and columns are there in the input.txt file beforehand.
  2. It is not necessary that all rows have the same number of columns.

That is, using std::vector is flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文