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
}
相关推荐
黑不溜秋的41 分钟前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
李白同学1 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?2 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农2 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿3 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!3 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴3 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程3 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
彳卸风3 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead4 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript