c  stl iota 和 atoi 用法示例详解-kb88凯时官网登录

来自:网络
时间:2024-09-10
阅读:
免费资源网,https://freexyz.cn/

一:功能

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

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

二:用法

#include 
#include 
#include 
int main() {
    std::vector 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
}
#include 
#include 
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
}
免费资源网,https://freexyz.cn/
返回顶部
顶部
网站地图