
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String::stod() function
The C++ std::string::stod() function is used to convert a string to a double floating point number. It analyse the initial characters of the string as a floating point number and returns its value. If the conversion is failed, it throws an invalid_argument exception.
If we take the value that is out of the range of representable values, it throws an out_of_range exception.
Syntax
Following is the syntax for std::string::stod() function.
double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0);
Parameters
This function has two parameters as described below.
- str − It indicates the string object with the representation of a floating point number.
- idx − It indicates the pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
Return value
This function returns the double type representing the converted value of the string.
Example 1
Following is the basic example to convert a string into double of std::stod() function using C++.
#include <iostream> #include <string> using namespace std; int main() { string number = "123.456"; double value = stod(number); cout << "The double value = " << value << endl; return 0; }
Output
If we run the above code it will generate the following output.
The double value = 123.456
Example 2
Following is an another example of std::stod() function used to extract a double value from a string that contains both text and numbers.
#include <iostream> #include <string> using namespace std; int main() { string mixedString = "Total: 250.75 USD"; size_t pos; double amount = stod(mixedString.substr(7), & pos); cout << "Amount = " << amount << endl; return 0; }
Output
If we run the above code it will generate the following output.
Amount = 250.75
Example 3
In this example, we have given a invalid input, so it will throw an invalid_argument exception because the string cannot be converted to double.
#include <iostream> #include <string> using namespace std; int main() { string invalidNumber = "abc"; try { double value = std::stod(invalidNumber); cout << "Value is: " << value << std::endl; } catch (const invalid_argument & e) { cout << "Invalid argument: " << e.what() << endl; } return 0; }
Output
Following is the output of the above code.
Invalid argument: stod