当前位置: 代码网 > it编程>编程语言>C/C++ > C++ STL iota 和 atoi 用法示例详解

C++ STL iota 和 atoi 用法示例详解

2024年08月03日 C/C++ 我要评论
一:功能 iota 是给定一个初始元素,然后依次对序列中每个元素进行递增++操作,详见代码一; atoi 是将字符串转换成整数;atol, atoll 将字符串转换成长整型数 long,l

一:功能

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

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

二:用法

#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
}
#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
}

到此这篇关于c++ stl iota 和 atoi 用法的文章就介绍到这了,更多相关c++ stl iota 和 atoi内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com