日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

最常见的读入数据方法集锦

發(fā)布時(shí)間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最常见的读入数据方法集锦 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我在程序編寫過程中,經(jīng)常會(huì)遇到讀入數(shù)據(jù)的問題,大概這類問題分為兩種,一種是從控制臺(tái)讀取,一類是從文件讀取,我這里收集了一些常見的讀取方法,以供參考。

控制臺(tái)讀取:

情景一、有一個(gè)程序要求我們輸入一個(gè)數(shù)組,數(shù)組的個(gè)數(shù)已給定或者要求先給出個(gè)數(shù),然后輸入數(shù)據(jù)。

代碼:

#include <iostream> #include <cmath> #include <vector> using namespace std;int main() {cout << "請(qǐng)輸入數(shù)組的個(gè)數(shù)" << " ";int n;cin >> n;int *a = new int[n];for (int i = 0; i < n;i++){cin >>a[i];}cout << "輸入的數(shù)據(jù)為" << " ";for (int i = 0; i < n; i++){cout <<a[i] << " ";}delete[]a;a = nullptr;return 0; }情景二、不斷輸入數(shù)字,然后求和

分析:這個(gè)問題的難點(diǎn)在于不知道輸入數(shù)組的個(gè)數(shù)。當(dāng)你輸入數(shù)字或者字符串后,回車,按ctrl+z結(jié)束輸入

代碼:

#include <iostream> #include <cmath> #include <vector> using namespace std;int main() {cout << "Enter numbers: ";int sum = 0;int input;while (cin >> input)sum += input;cout << "Last value entered = " << input << endl;cout << "Sum = " << sum << endl;return 0; }輸入:

Enter numbers: 45 78 45 ^Z Last value entered = 45 Sum = 168 請(qǐng)按任意鍵繼續(xù). . .

#include "iostream" #include "string" using namespace std; int main() {string word;while (getline(cin, word))cout << word << endl;return 0; }


輸入:

ajdskalld ajdskalld nacjkncklsa nacjkncklsa ^Z 請(qǐng)按任意鍵繼續(xù). . .


或者:

#include <iostream> #include <iterator> #include <algorithm> #include <vector> #include <string> using namespace std; int main() {istream_iterator< string > is(cin);istream_iterator< string > eof;vector< string > text;copy(is, eof, back_inserter(text));sort(text.begin(), text.end());ostream_iterator< string > os(cout, " ");copy(text.begin(), text.end(), os);return 0; }輸入:

acsnkalc acnkasm ^Z acnkasm acsnkalc 請(qǐng)按任意鍵繼續(xù). . .

情景三、讀入如下格式的數(shù)據(jù):

3 ? ? 5 ? ? ?6

5 ? ?6 ? ? ? 7

5 ? ?4 ? ? ? 4

即多行數(shù)據(jù),每行數(shù)據(jù)間以空格隔開。

#include <iostream> #include <vector> #include <sstream> #include <string> using namespace std;int main() {vector<string> stringlist;string str;cout << "請(qǐng)輸入數(shù)字,每行三個(gè)" << endl;while (getline(cin,str)){stringlist.push_back(str);}int data;for (int i = 0; i < stringlist.size();i++){stringstream s(stringlist[i]);s >> data;cout << data<<" ";s >> data;cout << data << " ";s >> data;cout << data << endl;}return 0; }輸入:

請(qǐng)輸入數(shù)字,每行三個(gè) 1 5 6 2 3 4 7 5 6 ^Z 1 5 6 2 3 4 7 5 6 請(qǐng)按任意鍵繼續(xù). . .


從文件讀取:

情景一、同樣是上述數(shù)據(jù),讀入文本數(shù)據(jù),并輸出。

3 ? ? 5 ? ? ?6

5 ? ?6 ? ? ? 7

5 ? ?4 ? ? ? 4


#include <iostream> #include <fstream> #include <iterator> #include <iostream> #include <vector> #include <sstream> #include <string> using namespace std;int main() {vector<string> stringlist;string str;ifstream infile("inputfile.txt");while (getline(infile, str)){stringlist.push_back(str);}int data;for (int i = 0; i < stringlist.size(); i++){stringstream s(stringlist[i]);s >> data;cout << data << " ";s >> data;cout << data << " ";s >> data;cout << data << endl;}return 0; }
參考文獻(xiàn):

1.如何判斷cin輸入結(jié)束?

2.【C++】輸入流cin方法

3.C++ stringstream介紹,使用方法與例子






總結(jié)

以上是生活随笔為你收集整理的最常见的读入数据方法集锦的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。