STL-string-1

news/2024/7/5 7:20:02

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

Convert string to integer

解析str,将其内容解释为指定基数的整数,该整数作为int值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 函数使用strtol(或wcstol)来执行转换(有关过程的更多详细信息,请参阅strtol)。

// stoi example
#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 << 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;
}Output:
2001, A Space Odyssey: 2001 and [, A Space Odyssey]
40c3:  16579
-10010110001: -1201
0x7f: 127

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

Convert string to long int

解析str,将其内容解释为指定基数的整数,该整数作为long int类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 函数使用strtol(或wcstol)来执行转换(有关过程的更多详细信息,请参阅strtol)。

// stol example
#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 << 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;
}Output:
1987520: 1987520
2f04e009: 788848649
-11101001100100111010: -956730
0x7fffff: 8388607

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

Convert string to unsigned integer

分析str,将其内容解释为指定基数的整数,该整数作为无符号长值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtoul(或wcstoul)来执行转换(有关该过程的更多详细信息,请参阅strtol)。

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

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

Convert string to long long

解析str,将其内容解释为指定基数的整数,该整数作为long-long类型的值返回。如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。该函数使用strtoll(或wcstoll)来执行转换(有关该过程的更多详细信息,请参阅strtol)。

// stoll example
#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;
}Output:
8246821 interpreted as 8246821
 0xffff interpreted as 65535
 020 interpreted as 16

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

Convert string to unsigned long long

分析str,将其内容解释为指定基数的整数,该整数作为unsigned long-long类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtoull(或wcstoull)来执行转换(有关该过程的更多详细信息,请参阅strtol)。

// stoull example
#include <iostream>   // std::cout
#include <string>     // 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;
}Possible output:
8246821 interpreted as 8246821
 0xffff interpreted as 65535
 020 interpreted as 16
 -1 interpreted as 18446744073709551615

stof

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

Convert string to float

分析str,将其内容解释为浮点数,该浮点数作为float类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtod(或wcstod)来执行转换(有关该过程的更多详细信息,请参阅strtod)。请注意,这些函数所接受的格式取决于当前的语言环境。

// stof example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stof

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;
}Possible output:
One martian year takes 1.88087 Earth years.

stod

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

Convert string to double

分析str,将其内容解释为浮点数,该浮点数作为double类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtod(或wcstod)来执行转换(有关该过程的更多详细信息,请参阅strtod)。请注意,这些函数所接受的格式取决于当前的语言环境。

// stod example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

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

  double earth = std::stod (orbits,&sz);
  double moon = std::stod (orbits.substr(sz));
  std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
  return 0;
}

Possible output:

The moon completes 12.3684 orbits per Earth year.

stold

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

Convert string to long double

分析str,将其内容解释为浮点数,该浮点数作为长双精度类型的值返回。 如果idx不是空指针,函数还会将idx的值设置为str中数字后面第一个字符的位置。 该函数使用strtell(或wcstold)来执行转换(有关该过程的更多详细信息,请参阅strtod)。

// stold example
#include <iostream>   // std::cout
#include <string>     // 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;
}Possible output:
Pluto takes 248.093 years to complete an orbit.

to_string

string to_string (int val);string to_string (long val);string to_string (long long val);string to_string (unsigned val);string to_string (unsigned long val);string to_string (unsigned long long val);string to_string (float val);string to_string (double val);string to_string (long double val);

Convert numerical value to string

返回一个以val表示的字符串。

// to_string example
#include <iostream>   // std::cout
#include <string>     // std::string, std::to_string

int main ()
{
  std::string pi = "pi is " + std::to_string(3.1415926);
  std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
  std::cout << pi << '\n';
  std::cout << perfect << '\n';
  return 0;
}Possible output:
pi is 3.141593
28 is a perfect number

to_wstring

wstring to_wstring (int val);wstring to_wstring (long val);wstring to_wstring (long long val);wstring to_wstring (unsigned val);wstring to_wstring (unsigned long val);wstring to_wstring (unsigned long long val);wstring to_wstring (float val);wstring to_wstring (double val);wstring to_wstring (long double val);

Convert numerical value to wide string

返回一个表示为val的wstring。

// to_wstring example
#include <iostream>   // std::wcout
#include <string>     // std::wstring, std::to_wstring

int main ()
{
  std::wstring pi = L"pi is " + std::to_wstring(3.1415926);
  std::wstring perfect = std::to_wstring(1+2+4+7+14) + L" is a perfect number";
  std::wcout << pi << L'\n';
  std::wcout << perfect << L'\n';
  return 0;
}Possible output:
pi is 3.141593
28 is a perfect number

http://lihuaxi.xjx100.cn/news/1197889.html

相关文章

视频美颜sdk是什么?技术解析与实现原理详解

视频美颜技术的发展则为人们提供了一种美化自己的方式&#xff0c;因此&#xff0c;视频美颜技术成为了一个备受关注的领域。在这个领域中&#xff0c;视频美颜sdk技术则是实现高效美颜的关键因素之一。本文将从技术角度分析视频美颜sdk的实现原理和优势。 一、视频美颜技术的…

虚拟现实 VR 智慧办公室可视化

“虚拟现实”是来自英文“Virtual Reality”&#xff0c;简称 VR 技术&#xff0c;其是通过利用计算机仿真系统模拟外界环境&#xff0c;主要模拟对象有环境、技能、传感设备和感知等&#xff0c;为用户提供多信息、三维动态、交互式的仿真体验。 图扑软件基于自研可视化引擎 H…

onnx模型转 ncnn 模型全连接层输出shape不对问题解决

1.简述 最近在把paddleocr 中cls分类模型通过ncnn部署框架部署时&#xff0c;发现onnx -> ncnn 模型的转换过程中出现问题。因为之前的项目都是使用ncnn框架部署的&#xff0c;只能去解决模型转换问题了。 2. 问题描述与分析 模型在onnx推理代码上正常&#xff0c;当把模型…

绝地求生 压q python版

仅做学习交流&#xff0c;非盈利&#xff0c;侵联删&#xff08;狗头保命) 一、概述 1.1 效果 总的来说&#xff0c;这种方式是通过图像识别来完成的&#xff0c;不侵入游戏&#xff0c;不读取内存&#xff0c;安全不被检测。 1.2 前置知识 游戏中有各种不同的q械&#xf…

这本数智平台白皮书讲透了大型企业数智化升级业务痛点

在以“升级企业数智化底座”为主题的2023用友BIP技术大会上&#xff0c;用友联合全球权威咨询机构IDC共同发布《建设数字中国 升级数智底座——企业数智化底座白皮书》&#xff0c;在这本数智平台白皮书里深入剖析了大型企业的数智化升级痛点。 大型企业普遍具有广域的业务覆盖…

什么是DAS/SAN/NAS

先上图 DAS DAS(Direct-attached Storage) 直连存储&#xff0c;这种存储设备通常是一个磁盘阵列柜&#xff0c;里面有多块磁盘&#xff0c;但不带RAID功能。 它与服务器主机之间的连接通常采用SCSI或者FC连接。DAS只能连接一台服务器&#xff0c;其它服务器无法共享该存储。 …

全民开发|零代码平台搭建库存管理系统,助力企业降本增效

什么是库存管理系统 库存管理系统是一种用于监控和跟踪商业组织存货的系统机软件。它帮助企业管理其库存&#xff0c;确保所需的商品和服务始终可用&#xff0c;并在需要时提供报告和预测。库存管理系统可追踪库存级别、库存周转率、订单、销售和交付等方面的数据&#xff0c;…

嘉兴桐乡外语提升辅导-四级考试倒计时了,还有救吗?

四级考试倒计时了&#xff0c;还有救吗&#xff1f; 即使复习时间再少&#xff0c;也不要放弃历年真题 不要盲目刷题&#xff0c;时间不够的话可以按年份由近到远的做&#xff0c;按考试时间严格要求自己&#xff0c;做完一套之后要及时对答案复盘&#xff0c;总结出自己错误的…