Convert String to size_t in C++
Last Updated :
28 Nov, 2022
To convert String to size_t in C++ we will use stringstream, It associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). We must include the stream header file in order to use stringstream. When parsing input, the stringstream class comes in quite handy.
Syntax:
std :: stringstream stream(string_name)
Example:
C++
#include <iostream>
using namespace std;
int main()
{
string s1 = "Hello Geek" ;
cout << s1 << endl;
string s2;
cin >> s2;
cout << s2 << endl;
return 0;
}
|
Output:
Hello Geek
GeeksforGeeks
Example:
C++
#include <iostream>
#include <stream>
#include <string>
using namespace std;
int main()
{
string str = "246810" ;
stringstream stream(str);
size_t output;
stream >> output;
cout << output << endl;
return 0;
}
|
Output:
246810
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...