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
}
相关推荐
qq_466302453 小时前
vs2008 Hotlink实时数据读取
c++·qt
代码or搬砖4 小时前
String字符串
android·java·开发语言
阿达King哥4 小时前
关于C++中的typedef typename的含义
c++
leo__5204 小时前
基于两步成像算法的聚束模式SAR MATLAB实现
开发语言·算法·matlab
Macbethad5 小时前
自动化测试技术报告
开发语言·lua
不会画画的画师5 小时前
Go开发指南:io/ioutil包应用和迁移指南
开发语言·后端·golang
2503_928411565 小时前
12.22 wxml语法
开发语言·前端·javascript
咔咔咔的5 小时前
3652. 按策略买卖股票的最佳时机
c++
5980354156 小时前
【java工具类】小数、整数转中文大写
android·java·开发语言
JIngJaneIL6 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot