std::stoi - std::stol - std::stoll

std::stoi - std::stol - std::stoll

Defined in header <string> - 定义于头文件 <string>

function template - 函数模板

转换字节字符串为整数值。

mutex:n. 互斥,互斥元,互斥体,互斥量
synchronization [ˌsɪŋkrənaɪˈzeɪʃn]:n. 同步,同时性
primitive [ˈprɪmətɪv]:adj. 原始的,远古的,简单的,粗糙的 n. 原始人
simultaneously [ˌsɪmlˈteɪniəsli]:adv. 同时地
exclusive [ɪkˈskluːsɪv]:adj. 独有的,排外的,专一的 n. 独家新闻,独家经营的项目,排外者
semantics [sɪˈmæntɪks]:n. 语义学,语义论
recursive [rɪˈkɜːsɪv]:adj. 递归的,循环的

1. std::stoi - std::stol - std::stoll

Convert string to integer - 转换字符串为 int

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

Convert string to long int - 转换字符串为 long int

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

Convert string to long long - 转换字符串为 long long

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);

Parses str interpreting its content as an integral number of the specified base, which is returned as an int - long int - long long value.
解析 str,将其内容解释为指定基数的整数,并将其作为 int - long int - long long 值返回。

If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.
如果 idx 不是空指针。

The function uses strtol (or wcstol) to perform the conversion (see strtol for more details on the process).
该函数使用 strtol (或 wcstol) 执行转换 (有关该过程的更多详细信息,请参见 strtol)。

舍弃所有空白符 (以调用 isspace() 鉴别),直到找到首个非空白符,然后取尽可能多的字符组成底 n (其中 n=base)的整数表示,并将它们转换成一个整数值。

合法的整数值由下列部分组成:

  • (可选) 正或负号
  • (可选) 指示八进制底的前缀 (0) (仅当底为 8 或 0 时应用)
  • (可选) 指示十六进制底的前缀 (0x0X) (仅当底为 16 或 0 时应用)
  • 一个数字序列
    底的合法集是 {0, 2, 3, ..., 36}。合法数字集对于底 2 整数是 {0, 1},对于底 3 整数是 {0, 1, 2},以此类推。对于大于 10 的底,合法数字包含字母字符,从对于底 11 整数的 Aa 到对于底36整数的 Zz。忽略字符大小写。

若 base 为 0 ,则自动检测数值进制:若前缀为 0 ,则底为八进制,若前缀为 0x0X,则底为十六进制,否则底为十进制。

若符号是输入序列的一部分,则对从数字序列计算得来的数字值取反,如同用结果类型的一元减。

idx 不是空指针,则指针 ptr - 转换函数内部者 - 将接收 str.c_str() 中首个未转换字符的地址,将计算该字符的下标并存储之于 *idx,该对象给出转换所处理的字符数。

2. Parameters

str
String object with the representation of an integral number.
要转换的字符串对象。

idx
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.
指向类型为 size_t 的对象的指针。

This parameter can also be a null pointer, in which case it is not used.
此参数也可以是空指针,在这种情况下不使用。

存储已处理字符数的整数的地址。

base
Numerical base (radix) that determines the valid characters and their interpretation.
确定有效字符及其解释的数字基 (基数)。

If this is 0, the base used is determined by the format in the sequence (see strtol for details). Notice that by default this argument is 10, not 0.
如果为 0,则使用的基数由序列中的格式确定 (有关详细信息,请参见 strtol)。请注意,默认情况下,此参数为 10,而不是 0。

数的底。

3. Return value

On success, the function returns the converted integral number as an int value.
成功后,函数将转换后的整数返回为 int - long int - long long 值。

对应 str 内容的整数值。

4. Example - 示例

4.1 std::stoi

//============================================================================
// Name        : std::stoi
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>   // std::cout
#include <string>     // 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 << "sz: " << sz << "\n";
	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;
}

sz: 4
2001, A Space Odyssey: 2001 and [, A Space Odyssey]
40c3: 16579
-10010110001: -1201
0x7f: 127

4.2 std::stol

//============================================================================
// Name        : std::stol
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>   // std::cout
#include <string>     // 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 << "sz: " << sz << "\n";
	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;
}

sz: 7
1987520: 1987520
2f04e009: 788848649
-11101001100100111010: -956730
0x7fffff: 8388607

4.3 std::stoll

//============================================================================
// Name        : std::stoll
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>   // std::cout
#include <string>     // 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;
}

8246821 interpreted as 8246821
 0xffff interpreted as 65535
 020 interpreted as 16

4.4 std::stoi - std::stol - std::stoll

//============================================================================
// Name        : std::stoll
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>

int main()
{
	std::string str1 = "45";
	std::string str2 = "3.14159";
	std::string str3 = "31337 with words";
	std::string str4 = "words and 2";

	int myint1 = std::stoi(str1);
	int myint2 = std::stoi(str2);
	int myint3 = std::stoi(str3);
	//	int myint4 = std::stoi(str4);
	//	terminate called after throwing an instance of 'std::invalid_argument'

	std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
	std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
	std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';

	return 0;
}

std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337

5. Complexity

Unspecified, but generally linear in the number of characters interpreted.
未指定,但通常线性。

6. Data races - 数据竞争

Modifies the value pointed by idx (if not zero).
修改由 idx 指向的值 (如果不为零)。

7. Exception safety - 异常安全性

If no conversion could be performed, an invalid_argument exception is thrown.
如果无法执行转换,则将抛出 invalid_argument 异常。

If the value read is out of the range of representable values by an int - long int - long long, an out_of_range exception is thrown.
如果读取的值超出 int - long int - long long 可表示的值的范围,则抛出 out_of_range 异常。

An invalid idx causes undefined behavior.
无效的 idx 导致未定义的行为。

Reference

http://www.cplusplus.com/reference/string/stoi/
http://www.cplusplus.com/reference/string/stol/
http://www.cplusplus.com/reference/string/stoll/
https://en.cppreference.com/w/cpp/string/basic_string/stol

发布了473 篇原创文章 · 获赞 1762 · 访问量 104万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104503710
std
今日推荐