📌  相关文章
📜  在C / C++中将字符串转换为数字

📅  最后修改于: 2021-05-26 00:16:44             🧑  作者: Mango

有两种将字符串转换为数字的常用方法:

使用stringstream类或sscanf()
stringstream():这是将数字字符串转换为int,float或double的简单方法。以下是使用stringstream将字符串转换为int的示例程序。

CPP
// A program to demonstrate the use of stringstream
#include 
#include 
using namespace std;
 
int main()
{
    string s = "12345";
 
    // object from the class stringstream
    stringstream geek(s);
 
    // The object has the value 12345 and stream
    // it to the integer x
    int x = 0;
    geek >> x;
 
    // Now the variable x holds the value 12345
    cout << "Value of x : " << x;
 
    return 0;
}


CPP
#include
int main()
{
    const char *str = "12345";
    int x;
    sscanf(str, "%d", &x);
    printf("\nThe value of x : %d", x);
    return 0;
}


CPP
// C++ program to demonstrate working of stoi()
// Work only if compiler supports C++11 or above.
#include 
#include 
using namespace std;
 
int main()
{
    string str1 = "45";
    string str2 = "3.14159";
    string str3 = "31337 geek";
 
    int myint1 = stoi(str1);
    int myint2 = stoi(str2);
    int myint3 = stoi(str3);
 
    cout << "stoi(\"" << str1 << "\") is "
         << myint1 << '\n';
    cout << "stoi(\"" << str2 << "\") is "
         << myint2 << '\n';
    cout << "stoi(\"" << str3 << "\") is "
         << myint3 << '\n';
 
    return 0;
}


CPP
// For C++11 above
#include 
#include 
using namespace std;
  
int main()
{
    const char *str1 = "42";
    const char *str2 = "3.14159";
    const char *str3 = "31337 geek";
    
    int num1 = atoi(str1);
    int num2 = atoi(str2);
    int num3 = atoi(str3);
   
    cout << "atoi(\"" << str1
              << "\") is " << num1 << '\n';
    cout << "atoi(\"" << str2
              << "\") is " << num2 << '\n';
    cout << "atoi(\"" << str3
              << "\") is " << num3 << '\n';
    
   return 0;
}


输出:

Value of x : 12345
// A stringstream is similar to input/output
// file stream. We need to declare a stringstream
// just like an fstream, for example: 
stringstream ss;

// and, like an fstream or cout, 
// we can write to it:
ss << myString; or 
ss << myCstring; or
ss << myInt;, or float, or double, etc.

// and we can read from it:
ss >> myChar; or
ss >> myCstring; or
ss >> myInt;  

总而言之,stringstream是一种方便的操作字符串。
sscanf()是类似于scanf()的C样式函数。它从字符串而不是标准输入中读取输入。

CPP

#include
int main()
{
    const char *str = "12345";
    int x;
    sscanf(str, "%d", &x);
    printf("\nThe value of x : %d", x);
    return 0;
}

输出:

Value of x : 12345

同样,我们可以分别使用%f和%lf读取float和double。

使用stoi()或atoi()进行字符串转换
stoi(): stoi()函数将字符串作为参数并返回其值。以下是一个简单的实现:

CPP

// C++ program to demonstrate working of stoi()
// Work only if compiler supports C++11 or above.
#include 
#include 
using namespace std;
 
int main()
{
    string str1 = "45";
    string str2 = "3.14159";
    string str3 = "31337 geek";
 
    int myint1 = stoi(str1);
    int myint2 = stoi(str2);
    int myint3 = stoi(str3);
 
    cout << "stoi(\"" << str1 << "\") is "
         << myint1 << '\n';
    cout << "stoi(\"" << str2 << "\") is "
         << myint2 << '\n';
    cout << "stoi(\"" << str3 << "\") is "
         << myint3 << '\n';
 
    return 0;
}

输出:

stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 geek") is 31337 

atoi(): atoi()函数采用字符数组或字符串字面量作为参数,并返回其值。以下是一个简单的实现:

CPP

// For C++11 above
#include 
#include 
using namespace std;
  
int main()
{
    const char *str1 = "42";
    const char *str2 = "3.14159";
    const char *str3 = "31337 geek";
    
    int num1 = atoi(str1);
    int num2 = atoi(str2);
    int num3 = atoi(str3);
   
    cout << "atoi(\"" << str1
              << "\") is " << num1 << '\n';
    cout << "atoi(\"" << str2
              << "\") is " << num2 << '\n';
    cout << "atoi(\"" << str3
              << "\") is " << num3 << '\n';
    
   return 0;
}

输出:

atoi("42") is 42
atoi("3.14159") is 3
atoi("31337 geek") is 31337 

stoi()vs atoi()

  • atoi()是旧的C样式函数。在C++ 11中添加了stoi()。
  • atoi()仅适用于C样式的字符串(字符数组和字符串字面量),stoi()适用于C++的字符串和C样式的字符串
  • atoi()仅接受一个参数,并返回整数值。
int atoi (const char * str); 
  • stoi()最多可以包含三个参数,第二个参数用于起始索引,第三个参数用于输入数字的基数。
int stoi (const string&  str, size_t* index = 0, int base = 10); 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”