How To Convert String To Int In C++?

Use `std::stoi`:

`int x = std::stoi(str);`

Use `std::stringstream`:

`std::stringstream ss(str);`

`int x; ss >> x;`

Use `std::atoi`:

`int x = std::atoi(str.c_str());`

Use `std::strtol`:

`int x = static_cast(std::strtol(str.c_str(), nullptr, 10));`

Use `std::from_chars`:

`int x; std::from_chars(str.data(), str.data() + str.size(), x);`

Suggested for You

Trending Today