【问题标题】:C++ convert int into string? [duplicate]C ++将int转换为字符串? [复制]
【发布时间】:2013-11-12 17:59:06
【问题描述】:

我尝试将 int 转换为 string ,但它不起作用我不知道为什么... 这是我的代码:

#include <stdio.h>
#include <stdlib.h>  
#include <iostream>
#include <array>


using namespace std;


int main()
{
    struct Studentendaten {
        int matrnr;
        string name;
        string vorname;
        string datum;
        float note;
    };
    Studentendaten stud;
    array<Studentendaten,100> studArray ;   

  FILE * pFile;
  int ch; 
  int mnr;
  string sub;
  string sub1;
  string sub2;
  string sub3;
  string sub4;
  string sub5;

  int i = 0;
  int pos ;

  pFile=fopen("studentendaten.txt","r");  
  if (pFile==nullptr) 
  {
      perror ("Fehler beim öffnen der Datei");
  }
  else
  {       
    while (ch != EOF) 
    {
      ch = fgetc(pFile);
      sub += (char)(ch);
      pos = sub.find(";");
      if (pos != -1) 
      {
          sub1 = sub.substr(0,pos);       
          sub2  = sub.substr(pos+1,pos);
          sub3  = sub.substr(pos+1,pos);
          sub4  =sub.substr(pos+1,pos);
          sub5  =sub.substr(pos+1,pos);       
          mnr   =stoi(sub1);
          stud.matrnr = mnr;
          stud.name = sub2;
          stud.vorname = sub3;
          stud.datum = sub4
          stud.note = float(sub5);
      }
      if (ch == '\n') 
      {
          stud = {matrn,name,vorname,datum,note};
          studArray.[i] = stud;
          i++;
      }


      putchar(ch);
    }   
    fclose (pFile);
    }


  return 0;
}

我试过 int mnr =stoi(sub1); 以及 int mnr = atoi(sub1); 其中 sub1 = "029383214" 类似的东西....为什么它不起作用?编译器报错...

【问题讨论】:

  • 我很困惑。你想要一个 int 到一个字符串还是一个字符串到一个 int?
  • atoi = ASCII 转整数。
  • 除了string,你的代码很像C。
  • 类型间转换最简单的方法是boost:lexical_cast
  • 这段代码大部分与问题无关。发布代码时,将其减少到可以显示问题的最小程度。

标签: c++ string compiler-construction int


【解决方案1】:

如果我在你的位置,我宁愿使用 boost::lexical_cast

string stringValue = lexical_cast<string>(101);
int intValue = lexical_cast<int>("101");

http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast.html

【讨论】:

    【解决方案2】:

    只需使用std::stringstream:

    int parse_int( const std::string& str )
    {
        std::stringstream ss( str );
        int value;
    
        if( ss >> value )
            return value;
        else
            throw;
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用字符串流:

      #include <sstream>
      ...
      
      // int to string
      
      int intVar = 10;
      
      std::stringstream out;
      out << intVar;
      std::string converted = out.str();
      
      // string to int
      
      std::string src = "555";
      std::stringstream in(str);
      
      int result = 0;
      in >> result;
      

      同时检查boost::lexical_cast

      【讨论】:

        【解决方案4】:

        使用std::to_string(int)

        Reference.

        【讨论】:

        • 使用 std::dateiLesen.cc:54:19 时出现错误:错误:'to_string' 不是 'std' 的成员 stud.matrnr = std::to_string(sub1) ;
        • @user2774480 您需要升级编译器以使用 C++x11 中的新 std::string 功能
        • 但我已经输入了编译器:g++ -std=c++11 -Wall -c "%f" 和 make g++ -std=c++11 -Wall -o "%e " "%f"
        【解决方案5】:

        您可以使用std::to_string 来处理简单的情况,或者使用std::stringstream 来控制格式(零填充、十六进制等)

        #include <iostream>
        #include <sstream>
        #include <iomanip>
        
        using namespace std;
        
        int main(int argc, const char * argv[]) {
            int value = 19;
        
            cout << to_string( value ) << endl;
        
            stringstream s1, s2;
            s1 << setfill('0') << setw(4) << value;
            s2 << "0x" << hex << setfill('0') << setw( 8 ) << value;
        
            cout << s1.str() << endl << s2.str() << endl;
        }
        

        【讨论】:

          猜你喜欢
          • 2010-09-17
          • 1970-01-01
          • 2020-08-27
          • 2021-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多