Text and Parsing Utilities
Parse Integer Text
std::stoi converts text containing digits into an integer value the program can calculate with.
stoi
`std::stoi` parses a string and returns an `int`.
parsed value
After parsing, the value behaves like any other integer.
Parse Integer Text
parse_integer_text.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>
int main() {
std::string text = "42";
int value = std::stoi(text);
int doubled = value * 2;
std::cout << "value=" << value << std::endl;
std::cout << "doubled=" << doubled << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string text = "7";
int value = std::stoi(text);
int doubled = value * 2;
std::cout << "value=" << value << std::endl;
std::cout << "doubled=" << doubled << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string text = "105";
int value = std::stoi(text);
int doubled = value * 2;
std::cout << "value=" << value << std::endl;
std::cout << "doubled=" << doubled << std::endl;
return 0;
}
text ← 42, value ← 42, doubled ← 84
4int main() {5 std::string text→ 42 = "42"; //@text="7", "105"67 int value→ 42 = std::stoi(text42);8 int doubled→ 84 = value42 * 2;910 std::cout << "value=" << value42 << std::endl;11 std::cout << "doubled=" << doubled84 << std::endl;12 return 0;13}outputvalue=42 doubled=84
text ← 7, value ← 7, doubled ← 14
4int main() {5 std::string text→ 7 = "7";67 int value→ 7 = std::stoi(text7);8 int doubled→ 14 = value7 * 2;910 std::cout << "value=" << value7 << std::endl;11 std::cout << "doubled=" << doubled14 << std::endl;12 return 0;13}outputvalue=7 doubled=14
text ← 105, value ← 105, doubled ← 210
4int main() {5 std::string text→ 105 = "105";67 int value→ 105 = std::stoi(text105);8 int doubled→ 210 = value105 * 2;910 std::cout << "value=" << value105 << std::endl;11 std::cout << "doubled=" << doubled210 << std::endl;12 return 0;13}outputvalue=105 doubled=210