C++ STL iota 和 atoi 用法

一:功能

iota 是给定一个初始元素,然后依次对序列中每个元素进行递增++操作,详见代码一;

atoi 是将字符串转换成整数;atol, atoll 将字符串转换成长整型数 long,long long。

二:用法

cpp 复制代码
#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> data(9, 0);
    for (auto v : data)
        std::cout << v << " ";
    std::cout << "\n";

    //对序列中元素进行累加, -4是初始值 
    std::iota(data.begin(), data.end(), -4); 
    for (auto v : data)
        std::cout << v << " ";
    std::cout << "\n";
    //4 -3 -2 -1 0 1 2 3 4
}
cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    printf("%i\n", atoi(" -123junk"));
    printf("%i\n", atoi(" +321dust"));
    printf("%i\n", atoi("0"));
    printf("%i\n", atoi("0042")); // treated as a decimal number with leading zeros
    printf("%i\n", atoi("0x2A")); // only leading zero is converted discarding "x2A"
    printf("%i\n", atoi("junk")); // no conversion can be performed
    printf("%i\n", atoi("2147483648")); // UB: out of range of int
}
相关推荐
zmzb0103几秒前
C++课后习题训练记录Day42
开发语言·c++·算法
不会编程的小寒5 分钟前
C / C++ 面试题
java·开发语言
坐吃山猪7 分钟前
Electron02-Hello
开发语言·javascript·ecmascript
Mr Lee_9 分钟前
Smali 文件生成dex装箱算法整合
开发语言·python·算法
QQ_43766431427 分钟前
线程池设计与实现
c++
姓刘的哦27 分钟前
RK3568开发板运行Qt
开发语言·qt
刘晓倩32 分钟前
Python3的Sequence
开发语言·python
ULTRA??41 分钟前
ROS Action 完整示例(AI辅助):客户端发目标 + 服务器接参数(lambda 替代 boost::bind)
c++·python
碧波bibo41 分钟前
【算法突破】【C++】 第一篇 数组
c++·算法
消失的旧时光-194342 分钟前
彻底理解 synchronized:实例锁、类锁与自定义锁的原理和最佳实践
java·开发语言