Using standard c++ to implement string and various types of conversion

  • 2020-04-02 01:44:07
  • OfStack

To achieve this goal, the stringstream class is the only one.

This class is defined in the header file, < sstream > The library defines three categories: istringstream, ostringstream, and stringstream, which are used for the input, output, and input/output operations of the stream, respectively. In addition, each class has a corresponding version of the wide character set.

For simplicity, I'll focus primarily on stringstream because each transformation involves input and output operations.

Example 1 demonstrates how to convert from string to int using a stringstream object note that instead of an array of characters, use a string object. This avoids the danger of a buffer overflow. Furthermore, the types of incoming parameters and target objects are automatically derived, even if the incorrect formatters are used.

Example 1:


std::stringstream stream;
string result="10000"; 
int n = 0; 
stream << result; stream >> n;//N is equal to 10000

Int to string conversion

string result;
int n = 12345; 
stream << n; 
result =stream.str();//The result is equal to "12345"

Reuse stringstream object if you intend to use the same stringstream object for multiple conversions, remember to use the clear() method before each conversion. The biggest benefit of using the same stringstream object for multiple conversions (rather than creating a new object each time) is efficiency. The construction and destructors of stringstream objects are often very CPU consuming. It turns out that using clear() alone does not clear the contents of the stringstream object, only the state of the object. To reuse the same stringstream object, you need to reinitialize the object using STR ().

Example 2:


std::stringstream strsql; 
for (int i= 1; i < 10; ++i) 
{ 
  strsql << "insert into test_tab values("; 
  strsql << i << ","<< (i+10) << ");";
   std::string str = strsql.str();//Get the string
   res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg); 
  std::cout << strsql.str() << std::endl; strsql.clear(); 
  strsql.str("");
}

Using templates in transformations also makes it easy to define function templates to convert an arbitrary type to a specific target type.

For example, you need to convert various numeric values, such as int, long, double, and so on, to strings using the to_string() function that takes a string type and an arbitrary value t as arguments.

The to_string() function converts t to a string and writes it to result.

Use the STR () member function to get a copy of the stream's internal buffer:

Example 3:


template void to_string(string & result,const T& t) 
{ ostringstream oss;//Create a stream oss<Out_type convert(const in_value & t)
{ stringstream stream; stream<>result;//Write a value return result to result; }

Convert () : double d; String salary; String s = "12.56"; D = the convert (s); //d = 12.56 salary=convert(9000.0); / / salary is equal to "9000"

Conclusion: The traditional form of conversion has been with us for a long time in the legacy code and pure C programs. However, as described in this article, stringstream-based transformations have such eye-catching features as type security and no overflows that there is a good reason to discard them < sstream > .

A better option is to use lexical_cast from the boost library, which is a type-safe conversion.

The following cases:


#include #include #include #include #include
 using namespace std; 
using namespace boost;
int main(void) 
try
{ 
//The following is a solution to the built-in string-to-type conversion
//Lexical_cast has obvious advantages
int ival;
char cval; 
ostringstream out_string;
string str0; 
string str1; 
ival = 100;
cval = 'w'; 
out_string << ival << " " << cval; 
str0 = out_string.str(); 
str1 = lexical_cast(ival) + lexical_cast(cval); 
cout << str0 << endl; cout << str1 << endl;
//The following is a string-to-built-in type conversion solution
//Lexical_cast is type safe almost compared to stringstrem,
int itmpe; 
char ctmpe;
str0 = "100k"; 
str1 = "100h";
istringstream in_string( str0 ); 
in_string >> itmpe >> ctmpe; 
cout << itmpe << " " << ctmpe << endl; 
itmpe = lexical_cast(str1);
ctmpe = lexical_cast(str1); 
system( "PAUSE" ); 
return 0;
} catch(bad_lexical_cast e) 
{ cout << e.what() << endl; cin.get(); }


Related articles: