자료형 변환
int
에서 string
string
에서 char
char
에서 int
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 10;
string nAsString = to_string(n);
for(const auto &item: nAsString) {
cout<<item<<": "<<typeid(item).name()<<endl;
}
for(const auto &item: nAsString) {
int temp = (int)item-48;
cout<<temp<<endl;
}
return 0;
}
~/De/D/C/P/Algorithms/A/TypeConvert main ❯ ./main.out 22:33:48
1: c
0: c
1
0
GitHub source code
- ex) stoi(string);
- stoi() - Convert string to integer (function template)
- stol() - Convert string to long int (function template)
- stoull() - Convert string to unsigned long long (function template)
- strtoll() - Convert string to long long integer (function)