c/c+ int2string2int atoi itoa atof

#define	_CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>

using namespace std;

void int2str(const int &int_temp, string &string_temp)
{
	char s[12]; //Setting 12 bits is enough to store 32-bit int values  
	_itoa(int_temp, s, 10); //The itoa function can also be implemented, but it belongs to the function in C, and the stream method is recommended in C++  
	string_temp = s;
}
void main()
{
	int a = 60;
	string str1 = "100";

	int2str(a,str1);
	cout << str1 << endl;

	string str2 = "278";
	int b = atoi(str2.c_str());

	cout << "b is " << b << endl;

}

When cout<<string type, use header file <string>, and namespace std.

2,  the differences between CString, string and string.h:

CSting: CString is an implementation in MFC or ATL. It is a powerful class encapsulated in MFC for string processing. Only projects that support MFC can use it. You don't need to add it yourself to use it in MFC, but you need to add #include<CString> in another program.

string: The string class is not only a standard C++ class library, but also a class library in STL (Standard Template Library, Standard Template Library), which has been included in the C++ standard. It is fundamentally different from CString.

string.h: The header file for the function definition of character arrays in C language. Common functions include strlen, strcmp, strcpy, etc. This header file has nothing to do with the C++ string class , so <string> is not <string.h > "upgrade version", they are unrelated to the two header files.


3.  Common type conversions such as int and string in C++ .

4, Conversion between int and string in C++


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325572288&siteId=291194637