0%

【C++】之 iota() 函数与 atoi() 函数详解

参考:

  1. https://blog.csdn.net/lixiaogang_theanswer/article/details/72588105
  2. http://c.biancheng.net/view/681.html

0.1 iota() 函数

  • C++ 中 iota() 是用来批量递增赋值 vector 的元素的。
    示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <numeric> //iota头文件
using namespace std;
void func() {
vector<int> v(10);
iota(v.begin(),v.end(),1);
vector<int>::iterator it = v.begin();
while(it != v.end())
{
cout<<*it++<<" ";
}
}

结果:
1 2 3 4 5 6 7 8 9 10
  • 定义在 numeric 头文件中的 iota() 函数模板会用连续的 T 类型值填充序列。前两个参数是定义序列的正向迭代器,第三个参数是初始的 T 值。第三个指定的值会被保存到序列的第一个元素中。保存在第一个元素后的值是通过对前面的值运用自增运算符得到的。当然,这意味着 T 类型必须支持 operator++()。下面展示了如何生成一个有连续的浮点值元素的 vector 容器:
    1
    2
    3
    4
    5
    6
    std::vector<double> data(9);
    double initial {-4};
    std::iota (std::begin (data) , std::end (data) , initial);
    std::copy(std::begin(data), std::end(data),std::ostream_iterator<double>{std::cout<< std::fixed << std::setprecision(1), " "});
    std::cout << std::endl;
    // -4.0 -3.0 -2.0 -1.0 0.0 1.0 2.0 3.0 4.0

以 -4 为初始值调用 iota() 会将 data 中元素的值设为从 -4 到 +4 的连续值。

  • 当然,初始值并不需要一定是整数:
1
2
std::iota(std::begin(data), std::end(data), -2.5);
// Values are -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 4.5 5.5

增量是 1,因此 data 中的值和注释显示的一样。

  • 可以将 iota() 算法应用到任意类型的序列上,只要它有自增运算符。下面是另一个示例:
1
2
3
4
string text {"This is text"};
std::iota(std::begin(text), std::end(text), 'K');
std::cout << text << std::endl;
// Outputs: KLMNOPQRSTUV

很容易看到输出如注释所示,字符串中的字符被设为以 K 开头的字符序列。这个示例发生了什么并不是那么明显:

1
2
3
4
5
std::vector<string> words (8);
std::iota(std::begin(words), std::end(words), "mysterious");
std::copy(std::begin(words), std::end(words),std::ostream_iterator<string>{std::cout, " "});
std::cout << std::endl;
// mysterious ysterious sterious terious erious rious ious ous

输出如注释所示。这是该算法的一个有趣应用,但没有什么用处。这只适用于第三个参数是一个字符串常量的情形。如果参数是 string{“mysterious”},将无法通过编译,因为没有为 string 类定义 operator++()。字符串常量对应的值是一个 const char* 类型的指针,可以将 ++ 运算符应用到它上面。因此对于 words 中第一个元素后的每个元素,指针的递增会导致字符串常量前面的字母被丢弃。将 ++ 应用到指针的结果是生成一个 string 对象,然后它会被保存到当前的元素序列中。

  • 只要 ++ 可以应用到序列中的元素类型上,就能将 iota() 算法应用到序列上。
  • 注意:很有趣的是,iota() 算法来源于 IBM 的编程语言 APL 中的 iota 运算符 ι。在 APL 中,表达式 ι10 会生成从 1 到 10 的整数的 vector。APL 是肯 • 艾弗森在 20 世纪 60 年代发明的。它是一门相当筒洁的语言,能够隐式处理 vector 和数组。APL 的一个完整程序会从键盘读取任意个值,计算出它们的平均值,然后输出被表示为 10 个字符结果。

0.2 atoi() 函数

  • atoi() 函数的功能是将字符串转换成整数 (int)。
  • 这两个函数名字很相似,容易混淆,注意区分;一个在用在 c++ 的 STL 中,一个是用在 c 中。
1
2
#include <stdlib.h> 
int atoi(const char *str);
  • 功能:atoi() 会扫描 str 字符串,跳过前面的空格字符,直到遇到数字或正负号才开始做转换,而遇到非数字或字符串结束符 (’\0’) 才结束转换,并将结果返回返回值。
  • 参数:
    str:待转换的字符串
    【返回值】返回转换后的整型数;如果 str 不能转换成 int 或者 str 为空字符串,那么将返回 0
  • 示例:
1
2
3
4
5
6
7
8
9
10
void func() {
char str1[] = "-10";
int num1 = atoi(str1);
printf("num1 = %d\n", num1);
}

//结果:
num1 = -10

char str1[] = "abc-1100def";结果是: num1 = 0
  • 本文作者: FXJFXJ
  • 本文链接: https://fxj.wiki/posts/6aea0693/
  • 版权声明: 本博客所有文章除特别声明外,均采用 ZERO 许可协议。转载请注明出处!