C++?STL?iota?和?atoi?用法示例詳解
更新時間:2024年08月03日 09:42:44 作者:黑不溜秋的
atoi是一個C/C++標準庫中的函數,用于將一個以ASCII字符串表示的整數轉換為整數類型,這篇文章主要介紹了C++?STL?iota?和?atoi?用法,需要的朋友可以參考下
一:功能
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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Visual Studio 2019創(chuàng)建C++ Hello World項目的方法
這篇文章主要介紹了Visual Studio 2019創(chuàng)建C++ Hello World項目的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03

