Several methods of conversion between C++ numbers and strings

Usually when writing questions, we always encounter the problem of how to convert numbers and strings. If we use general methods.

The regular routine of
changing a character string into a number. Qin Jiuzhao's algorithm
res=res*base+digit of each digit (from high to low)

string a;
int res=0;
for(int i=0;i<a.length();i++)
res=res*10+a[i]-'0';

Numbers become strings.
This is how to change the number 19234 into a string

#include<iostream>
#include<algorithm>

using namespace std;

int main(void)
{
    
    
	int s=19234;
	string a;
	while(s)
	{
    
    
		a+=s%10+'0';
		s/=10;
	}
	reverse(a.begin(),a.end() );
	cout<<a;
}

The method of turning a number into a string uses the reverse function. I think that the amount of code is very large and it is not worth using. Do we have a simpler method? Yes.

Then in the first ten days of the winter vacation, we took an online class, and we learned a very good solution, called string streaming.

stringstream this variable name

Then this is in the sstream header file, how should we use it.

#include<iostream>
#include<algorithm>
#include<sstream>

using namespace std;

int main(void)
{
    
    
	string a="19235";
	int digit;
	stringstream ss;
	ss<<a;
	ss>>digit;
	cout<<digit;
}

Insert picture description here
Precautions
The original string cannot have spaces, if spaces appear

#include<iostream>
#include<algorithm>
#include<sstream>

using namespace std;

int main(void)
{
    
    
	string a="192 35";
	int digit;
	stringstream ss;
	ss<<a;
	ss>>digit;
	cout<<digit;
}

Insert picture description here
When we are doing the questions, there may be some situations like this.
Insert picture description here
Example question AcWing 1204. Wrong ticket When
you enter the number of each line, we don't know how many.
We can use this stringstream

#include<iostream>
#include<algorithm>
#include<sstream>

using namespace std;

const int N=1e5+10,Inf=1e7;

int st[N];

int main(void)
{
    
    
        int i=0;
        int n;
        scanf("%d\n",&n);
        cout<<n<<endl;
        string a;
        while(getline(cin,a))
        {
    
    
        stringstream ss(a);
        i=0;
        while(ss>>st[i]) i++;
        for(int j=0;j<i;j++)
        printf("%4d ",st[j]);
        puts("");
        }
        
}


Insert picture description here
We can observe that every number is output, and then how do we use it.

1. Preprocessing, we need to use the sstream header file.

#include<sstream>

Two, initialization

stringstream ss;

If it's in this form

stringstream ss(a);

Equivalent to

stringstream ss;
ss<<a;

These are two methods of initialization.
three,

int digit;
double digi;
float dig;

Choose the type of number you want to transform.

ss>>digit;
ss>>digi;
ss>>dig;

To achieve conversion, let's talk about it at this time. When ss>>digit is converted, it will return 0 if it is unsuccessful.
So we can achieve such a process.

while(ss>>digit[i]) i++;
把ss保存的多个数保存在digit数组里

This is the usage of stringstream that we need to learn.

Continue to update this article~~, the next step is to introduce the usage of sscanf and the usage of ssprintf

Guess you like

Origin blog.csdn.net/qq_52358098/article/details/114166389