我总是无缘无故地抛出 'std::invalid_argument' what(): stoi 异常?

I keep getting the 'std::invalid_argument' what(): stoi exception thrown for seemingly no reason?

我正在尝试读取具有以下格式的文件:1,2,4,6,8,12,12, .我想使用 getline() 和一个 stringstream 对象来分隔输入并在转换为整数后将其存储到一个向量中。它有效,我可以看到输出能够与数字 1 相加,但它在完成转换文件中的所有数字后仍然抛出异常。

输出: 2个 3个 5个 7 9 13 13

#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

int main (int argc, char** argv){
    ifstream infile;
    vector <int> vect;
    infile.open("tested");
    if(!infile.is_open()){
        cout<<"File did not open"<<endl;
    }
    else{
        while(!infile.eof()) {
            string line;
            getline(infile, line);
            stringstream ss(line);
            while (ss){
                string p;
                getline(ss, p, ',');
                int x = stoi(p);
                cout<<x+1<<endl;
                vect.push_back(x);
            }
        }
        int i=0;
        while(i<vect.size()){
            int e = vect[i];
            cout<<e<<endl;
            i++;
        }
        sort(vect.begin(), vect.end());
        int j=0;
        while(j<vect.size()){
            int n = vect[j];
            cout<<n<<endl;
            j++;
        }
        cout<<"end reached"<<endl;
    }
}

关于我的评论,您在 getline() 中使用了定界符,所以当您的最后一个数字没有行时会发生什么情况,它会引发异常。因为 std::stoi 没有任何转换。

所以我只是在 getline() 为真时循环。

#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>

int main (int argc, char **argv)
{
  std::ifstream infile;
  std::vector<int> vect;
  infile.open ("tested", std::ios::in);
  if (!infile.is_open ())
    {
      std::cout << "File did not open" << std::endl;
    }
  else
    {
      std::string p;
      while (!infile.eof ())
        {
          std::string line;
          getline (infile, line);
          std::stringstream ss (line);
          // Changed
          while (getline (ss, p, ','))
            {
              int x = stoi (p);
              // std::cout << x + 1 << std::endl;
              vect.push_back (x);
            }
        }
      int i = 0;
      while (i < vect.size ())
        {
          int e = vect[i];
          std::cout << e << std::endl;
          i++;
        }
      sort (vect.begin (), vect.end ());
      int j = 0;
      while (j < vect.size ())
        {
          int n = vect[j];
          std::cout << n << std::endl;
          j++;
        }
      std::cout << "end reached" << std::endl;
    }
}

您的代码比需要的更复杂。您根本不需要使用 std::stoi()。由于您已经在使用 std::stringstream,只需让它为您解析整数即可。

#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

int main (){
    ifstream infile("tested");
    if (!infile.is_open()){
        cout << "File did not open" << endl;
    }
    else{
        vector<int> vect;
        string line;
        while (getline(infile, line)) {
            istringstream iss(line);
            int x; char c;
            while (iss >> x) {
                cout << x + 1 << endl;
                vect.push_back(x);
                iss >> c;
            }
        }
        for (size_t i = 0; i < vect.size(); ++i){
            cout << vect[i] << endl;
        }
        sort(vect.begin(), vect.end());
        for (int j = 0; j < vect.size(); ++j){
            cout << vect[j] << endl;
        }
        cout << "end reached" << endl;
    }
    return 0;
}

Live Demo