std::to_string

项目中遇到一个要将int转成std::string的使用,看到前面同事是通过stringstream实现的,感觉有点麻烦,实现方式如下:

#include 
#include 
#include 
using namespace std;

int main()
{
	int sn = 33;
	std::string s_user = "admin";
	std::string s_str;
	std::stringstream ss;
	
	ss.str("");
	ss << sn;
	ss >> s_str;

	s_str += s_user;
	cout << "s_str:" << s_str;
	return 0;
}

通过搜索int转string发现了to_string()

to_string()实现:

#include 
#include 
using namespace std;

int main()
{
	int sn = 33;
	std::string s_user = "admin";
	std::string s_str;
	
	s_str = std::to_string(sn) + s_user;
	cout << "s_str:" << s_str;
	return 0;
}

1.int转string

1.1 c++11标准增加了全局函数std::to_string:
string to_string (int val);

string to_string (long val);

string to_string (long long val);

string to_string (unsigned val);

string to_string (unsigned long val);

string to_string (unsigned long long val);

string to_string (float val);

string to_string (double val);

string to_string (long double val);

示例一:

// to_string example
#include    // std::cout
#include      // std::string, std::to_string
 
int main ()
{
  std::string pi = "pi is " + std::to_string(3.1415926);
  std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
  std::cout << pi << '\n';
  std::cout << perfect << '\n';
  return 0;
}

输出:

pi is 3.141593
28 is a perfect number
1.2 采用sstream中定义的字符串流对象来实现
#include    // std::cout
#include      // std::string, std::to_string
#include 	  // std::ostringstream
using namespace std;
int main ()
{
	ostringstream os; //构造一个输出字符串流,流内容为空 
	int i = 12; 
	os << i; //向输出字符串流中输出int整数i的内容 
	cout << os.str() << endl; //利用字符串流的str函数获取流中的内容 
  	return 0;
}

这里利用ostringstream实现,其实和利用stringstream同理;

C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。

istringstream类用于执行C++风格的串流的输入操作。

ostringstream类用于执行C风格的串流的输出操作。

stringstream类同时可以支持C风格的串流的输入输出操作。

具体使用可参考:https://www.jb51.net/article/100691.htm

2.string转int

2.1 c++11标准使用std::stoi/stol/stoll等函数:
#include 
#include 
 
int main()
{
    std::string str1 = "45";
    std::string str2 = "3.14159";
    std::string str3 = "31337 with words";
    std::string str4 = "words and 2";
 
    int myint1 = std::stoi(str1);
    int myint2 = std::stoi(str2);
    int myint3 = std::stoi(str3);
    // error: 'std::invalid_argument'
    // int myint4 = std::stoi(str4);
 
    std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
    std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
    std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
    //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
}

输出:

std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
2.2 采用标准库中atoi函数,对于其他类型也都有相应的标准库函数,比如浮点型atof(),long型atol()等等
string s = "12"; 
int a = atoi(s.c_str());
2.3 采用sstream头文件中定义的字符串流对象来实现转换
istringstream is("12"); //构造输入字符串流,流的内容初始化为“12”的字符串 
int i; 
is >> i; //从is流中读入一个int整数存入i中

参考资料:
https://blog.csdn.net/u010510020/article/details/73799996
https://zh.cppreference.com/w/cpp/string/basic_string/to_string

你可能感兴趣的:(C++,to_string,string,字符串)