C++?STL?iota?和?atoi?用法示例詳解
一:功能
iota 是給定一個初始元素,然后依次對序列中每個元素進行遞增++操作,詳見代碼一;
atoi 是將字符串轉(zhuǎn)換成整數(shù);atol, atoll 將字符串轉(zhuǎn)換成長整型數(shù) 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
}到此這篇關(guān)于C++ STL iota 和 atoi 用法的文章就介紹到這了,更多相關(guān)C++ STL iota 和 atoi內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言 數(shù)據(jù)結(jié)構(gòu)雙向鏈表簡單實例
這篇文章主要介紹了C語言 數(shù)據(jù)結(jié)構(gòu)雙向鏈表簡單實例的相關(guān)資料,需要的朋友可以參考下2017-03-03
Visual Studio 2019創(chuàng)建C++ Hello World項目的方法
這篇文章主要介紹了Visual Studio 2019創(chuàng)建C++ Hello World項目的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
C語言中send()函數(shù)和sendto()函數(shù)的使用方法
這篇文章主要介紹了C語言中send()函數(shù)和sendto()函數(shù)的使用方法,是C語言入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
C語言動態(tài)內(nèi)存管理的實現(xiàn)示例
動態(tài)內(nèi)存管理是一種允許程序在運行時根據(jù)需要動態(tài)申請和回收內(nèi)存的策略,它提供了四種重要的函數(shù),本文就來介紹一下,感興趣的可以了解一下2024-11-11

