std::stox类型

std::stod

函数原型

double stod (const string&  str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);

函数功能
将std::string字符串转化为双精度类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置

函数返回值
如果成功则返回转换的double型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

这里说明一下参数idx的意义和作用:
如果当我们的字符串中只有一个double型数值时我们可以这样进行转化:

#include 
#include 

int main()
{
	std::string numStr = "5646545.32";

	double number1 = std::stod(numStr);
	return 0;
}

但是如果字符串中有两个double型的数值该如何进行转化呢?这个时候我们就可以使用参数idx存储上一个double数值解析完成之后的在字符串中的索引,代码如下:

#include 
#include 

int main()
{
	std::string numStr = "5646545.32 3.1415923";
	size_t idx = 0;

	double number1 = std::stod(numStr, &idx);
	double number2 = std::stod(numStr.substr(idx));

	return 0;
}

std::stof

函数原型

float stof (const string&  str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);

函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。

函数返回值
如果成功则返回转换的float型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

#include 
#include 

int main()
{
	std::string orbits("686.97 365.24");
	std::string::size_type sz;     // alias of size_t

	float mars = std::stof(orbits, &sz);
	float earth = std::stof(orbits.substr(sz));
	std::cout << "One martian year takes " << (mars / earth) << " Earth years.\n";
	return 0;
}

注意float的有效位为6-7位,如果超过的话,精度会丢失

std::stoi

函数原型

int stoi (const string&  str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);

函数功能
将std::string字符串转换为int类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。
base  转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的int型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include    // std::cout
#include      // std::string, std::stoi

int main()
{
	std::string str_dec = "2001, A Space Odyssey";
	std::string str_hex = "40c3";
	std::string str_bin = "-10010110001";
	std::string str_auto = "0x7f";

	std::string::size_type sz;   // alias of size_t

	int i_dec = std::stoi(str_dec, &sz);
	int i_hex = std::stoi(str_hex, nullptr, 16);
	int i_bin = std::stoi(str_bin, nullptr, 2);
	int i_auto = std::stoi(str_auto, nullptr, 0);

	std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
	std::cout << str_hex << ": " << i_hex << '\n';
	std::cout << str_bin << ": " << i_bin << '\n';
	std::cout << str_auto << ": " << i_auto << '\n';

	return 0;
}

std::stox类型_第1张图片

std::stol

函数原型

long stol (const string&  str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);

函数功能
将std::string字符串转换为long int类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。
base  转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的long int型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include    // std::cout
#include      // std::string, std::stol

int main()
{
	std::string str_dec = "1987520";
	std::string str_hex = "2f04e009";
	std::string str_bin = "-11101001100100111010";
	std::string str_auto = "0x7fffff";

	std::string::size_type sz;   // alias of size_t

	long li_dec = std::stol(str_dec, &sz);
	long li_hex = std::stol(str_hex, nullptr, 16);
	long li_bin = std::stol(str_bin, nullptr, 2);
	long li_auto = std::stol(str_auto, nullptr, 0);

	std::cout << str_dec << ": " << li_dec << '\n';
	std::cout << str_hex << ": " << li_hex << '\n';
	std::cout << str_bin << ": " << li_bin << '\n';
	std::cout << str_auto << ": " << li_auto << '\n';

	return 0;
}

std::stox类型_第2张图片

std::stold

函数原型

long double stold (const string&  str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);

函数功能
将std::string字符串转换为long double类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置

函数返回值
如果成功则返回转换的long double型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include    // std::cout
#include      // std::string, std::stod

int main()
{
	std::string orbits("90613.305 365.24");
	std::string::size_type sz;     // alias of size_t

	long double pluto = std::stod(orbits, &sz);
	long double earth = std::stod(orbits.substr(sz));
	std::cout << "Pluto takes " << (pluto / earth) << " years to complete an orbit.\n";
	return 0;
}

std::stoll

函数原型

long long stoll (const string&  str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);

函数功能
将std::string字符串转换为long long类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置
base  转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的long long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include    // std::cout
#include      // std::string, std::stoll

int main()
{
	std::string str = "8246821 0xffff 020";

	std::string::size_type sz = 0;   // alias of size_t

	while (!str.empty()) {
		long long ll = std::stoll(str, &sz, 0);
		std::cout << str.substr(0, sz) << " interpreted as " << ll << '\n';
		str = str.substr(sz);
	}

	return 0;
}

在这里插入图片描述

std::stoul

函数原型

unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);

函数功能
将std::string字符串转换为unsigned long类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。
base 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的unsigned long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

#include    // std::cin, std::cout
#include      // std::string, std::stoul, std::getline

int main ()
{
  std::string str;
  std::cout << "Enter an unsigned number: ";
  std::getline (std::cin,str);
  unsigned long ul = std::stoul (str,nullptr,0);
  std::cout << "You entered: " << ul << '\n';
  return 0;
}

在这里插入图片描述

std::stoull

函数原型

unsigned long long stoull (const string&  str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);

函数功能
将std::string字符串转换为unsigned long long类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置
base 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的unsigned long long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

#include    // std::cout
#include      // std::string, std::stoull

int main()
{
	std::string str = "8246821 0xffff 020 -1";

	std::string::size_type sz = 0;   // alias of size_t

	while (!str.empty()) {
		unsigned long long ull = std::stoull(str, &sz, 0);
		std::cout << str.substr(0, sz) << " interpreted as " << ull << '\n';
		str = str.substr(sz);
	}

	return 0;
}

std::stox类型_第3张图片

你可能感兴趣的:(C++入门,c++,算法,开发语言)