STL-string-1

这篇具有很好参考价值的文章主要介绍了STL-string-1。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

STL-string-1

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。文章来源地址https://www.toymoban.com/news/detail-468475.html

// 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

到了这里,关于STL-string-1的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python中出现could not convert string to float:的问题

    GREENBIRD的个人絮絮念 可能情况 列表中并非纯数字,混杂着字母,当你定义一个np.zeros用于存储这个列表时报错 改成以下解决问题 2.读取文本数据中出现回车换行,导致出现[\\\' \\\']行,存入np.zeros所定义的矩阵报错 listFromLine[0:4]输出如下,因为有空格的缘故,导致数组returnMat中的

    2024年02月11日
    浏览(37)
  • 机器学习时出现 could not convert string to float:‘xxx‘解决方法

    先放结论:数据未进行One hot code 解决方法:使用这个函数pd.get_dummies()对数据进行处理 案例:  因为:    因为类型不能转换为float等数字类型,不是数字直接进行机器学习是不行的,同理直接进行归一化、标准化同样不行。报错相同。  加入函数 不报错。 看看One_hot_encode化

    2024年02月11日
    浏览(29)
  • 【Python】成功解决ValueError: could not convert string to float: ‘ ignoring input‘

    【Python】成功解决ValueError: could not convert string to float: ’ ignoring input’ 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平均质量分92+),分享更

    2024年04月15日
    浏览(41)
  • RabbitMQ Failed to convert message.No method found for class java.lang.String问题解决

            org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message         Caused by: org.springframework.amqp.AmqpException: No method found for class java.lang.String 1、消息生产者发送的消息类型为String,消息消费者接收的消息类型为Message,导致接收的时候类型转换不

    2024年02月16日
    浏览(50)
  • 解决Failed to convert value of type ‘java.lang.String‘ to required type ‘java.lang.Integer

    项目:网上商城练习 问题:使用postman测试接口报错:类型转换异常 上代码: 改为: 直接去掉{}和@PathVariable注释,容易找不到对应的参数类型,希望对大家有用,问题已解决。

    2024年02月11日
    浏览(44)
  • 报错信息Failed to convert value of type ‘java.lang.String‘ to required type ‘java.lang.Integer‘

    2.1 从前端查看接口 根据报错信息它的信息大概是前台给我传了一个string类型的listAllTag不能转换成Integer,我看了半天也没能想到为什么他会传给我一个String的字符串因为这个接口就是简单的获取一个list集合返回,很棒前台接口也是报500。 2.2查看后端接口 就把重点放在了Contro

    2024年02月11日
    浏览(83)
  • Java时间转换问题 [Failed to convert property value of type ‘java.lang.String‘ to required type ‘java.

    default message [Failed to convert property value of type ‘java.lang.String’ to required type \\\'java. 遇到java接收前端日期字符串返回到后端Date字段时报错。 通过在报错字段上添加@DateTimeFormat(pattern = “yyyy-MM-dd”)进行解决。 接下来是分析了引用一位博主的博客,我在简单总结一下: @JsonFormat注

    2024年02月13日
    浏览(30)
  • 【剖析STL】String

    标准模板库(Standard Template Library,STL)是惠普实验室开发的一系列软件的统称。它是由Alexander Stepanov、Meng Lee和David R Musser在惠普实验室工作时所开发出来的。虽说它主要出现到C++中,但在被引入C++之前该技术就已经存在了很长时间。STL的代码从广义上讲分为三类:algorithm(

    2024年02月04日
    浏览(29)
  • 【STL】string类 (下)

    目录 1,insert 2,erase 3,find 4,replace 5,rfind 6,substr 7,find_first_of 8,find_first_not_of 9,find_last_of 10,operator+ 11,getline 在 pos 位置之前插入字符串 擦除范围字符串 从 pos 位置开始,用 n 个字符替换; 上述可以看到,第一个替换从下标 0 开始用两个字符也就是 ” he “ 替换 “

    2024年02月05日
    浏览(57)
  • 【STL】string的使用

    放在专栏【 C++知识总结 】,会持续更新,期待支持 🌹 STL为英文Standard Template Library的缩写,译为 标准模板库 。 是C++标准库的重要组成部分 。 长久以来,软件届一直希望建立一种可重复运用的东西。所谓的泛型思想以及面向对象最主要的目的就是为了复用性的提升 复用性

    2024年02月05日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包