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
}
相关推荐
软行12 分钟前
LeetCode 每日一题 3341. 到达最后一个房间的最少时间 I + II
数据结构·c++·算法·leetcode·职场和发展
nlog3n21 分钟前
Go语言交替打印问题及多种实现方法
开发语言·算法·golang
kaixin_learn_qt_ing25 分钟前
Golang
开发语言·后端·golang
ddd...e_bug28 分钟前
Shell和Bash介绍
开发语言·bash
代码AC不AC1 小时前
【C++】STL简介
c++·stl简介
C4程序员1 小时前
Java百度身份证识别接口实现【配置即用】
java·开发语言
unityのkiven1 小时前
C++中的虚表和虚表指针的原理和示例
开发语言·c++
炒空心菜菜1 小时前
MapReduce 实现 WordCount
java·开发语言·ide·后端·spark·eclipse·mapreduce
(・Д・)ノ1 小时前
python打卡day27
开发语言·python
火山灿火山1 小时前
【简单模拟实现list】
c++