Files
CPlusPlusThings/extension/some_problem/README.md
light-city dfe1c933b3 docs: address community issue feedback (#5 #338 #142 #246 #136 #303)
- const README: rewrite the space-saving claim per issue #5 follow-ups
  (constant-expression const vs #define immediates; which const qualify
  as constant expressions)
- const README: fix the class-section diagnosis per issue #338 (overload
  resolution picks the const overload; the restriction only surfaces
  once that overload is removed)
- sizeof README: virtual-inheritance result is compiler-dependent
  (GCC merges vbptr+vfptr -> 16, MSVC keeps them separate -> 32) #246
- static README: new section on internal linkage / access restriction
  with anonymous-namespace comparison #142
- 10-day practice: full problem list table + fixed day links #136
- new design_pattern/singleton/singleton_default.hpp: reusable
  thread-safe CRTP singleton template, compile+concurrency tested #303
2026-10-13 19:23:54 +08:00

100 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
> 本文档由 [C++那些事网站](https://light-city.github.io/stories_things/cpp-tools/cpp-string-to-int/) 同步生成,网站版本含多语言与更新排版。
# C++ string 转 int
个人公众号:
![](https://raw.githubusercontent.com/Light-City/CPlusPlusThings/master/img/wechat.jpg)
首先提出解决方案:
- atoi
- strtol
- stoi
这几个有什么不同呢?下面测试对比。
> C语言风格函数
atoi与strtol对比:
```cpp
string str = "16s";
int a = atoi(str.c_str());
int b = strtol(str.c_str(), nullptr, 10);
```
输出:
```
atoi的结果为:16
strtol的结果为:16
```
这两个函数都是从字符串开始寻找数字或者正负号或者小数点,遇到非法字符终止。
所以到上述s字符就不输出了,提前结束,也就是说当你的字符串不是数字的时候,或者小数点等非数字,不会报异常!直接输出0.
例如:
```cpp
string str = "asdsa";
int a = atoi(str.c_str());
int b = strtol(str.c_str(), nullptr, 10);
```
输出:
```
0
0
```
strtol相比与atoi来说,支持多种进制转换,例如8进制等
例如:
```
int b = strtol(str.c_str(), nullptr, 8);
```
> C++风格
在C++中可以使用stoi来转int,这个函数相比于前两个一个最大特点是:异常!
我们知道C++相比于C语言多了异常,这也是这个函数在C++中具有的最显著功能。
例如:
```cpp
string str1 = "asq,";
// int c = stoi(str1); // 报异常
string str2 = "12312";
int c = stoi(str2); // ok
cout << c << endl;
```
异常如下:
```
terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
```
> 自定义
也就是自己写呗,如下:
```cpp
int stringToInt(const string &s) {
int v;
stringstream ss;
ss << s;
ss >> v;
return v;
}
int main() {
int i = stringToInt("2.3");
cout<<i<<endl;
}
```