1.头文件
一些常用的头文件
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
using namespace std;
C++万能头文件,但编译不一定能支持
#include<bits/stdc++.h>
它的源码
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
2.输入与输出
(1)cin
[输入]cin是C++中, 标准的输入流对象,下面列出cin的两个用法,单独读入,和批量读入
cin的原理,简单来讲,是有一个缓冲区,我们键盘输入的数据,会先存到缓冲区中,用cin可以从缓冲区中读取数据。
注意1:cin可以连续从键盘读入数据
注意2:cin以空格、tab、换行符作为分隔符
注意3:cin从第一个非空格字符开始读取,直到遇到分隔符结束读取
(2)getline()
[输入]从cin的注意中,也可以看出,当我们要求读取的字符串中间存在空格的时候,cin会读取不全整个字符串,这个时候,可以采用getline()函数来解决。
cin.getline(name,256); //读取最多256个字符到name中
cin.getline(name,6,'#'); //限定长度与结束符,截断输出
getline(cin,name); // '\n'或EOF结束符
getline(cin,name,'#'); //'#'作为结束符
(3)getchar()
[输入]该函数会从缓存区中读出一个字符,经常被用于判断是否换行
(4)cout
[输出]用于标准输出,与cin相对应。
(5)其他
当然,C++中的输入输出函数不止这几个,其他的输入函数包括scanf(),cin.get()等等方式,输出函数也有printf(),clog,cerr等方式,要根据具体的使用场景,选择具体的输入输出函数。
3.应用
3.1整型输入
#include <iostream>
#include <vector>
using namespace std;
void intFun1_1();void intFun1_2();
void intFun2();
void intFun3();
void intFun4();
int main(){
intFun4();
return 0;
}
/*在终端的一行输入固定数目的整型数字,并存到数组中,中间以空格分隔*/
//示例: //
//3 //
//1 2 3 //
/* */
void intFun1_1(){
int n;
cin>>n;
vector<int> nums(n);//或者 vector<int> nums;nums.resize(n);
for(int i = 0 ;i<n;++i){
cin>>nums[i];
}
/*test*/
cout<<"test C++ input:"<<endl;for(auto i:nums) {cout<<i<< " " ;}cout<<endl;
}
void intFun1_2(){
int n;
cin>>n;
vector<int> nums;
for(int i = 0 ;i<n;++i){
int val;
cin>>val;
nums.push_back(val);
}
/*test*/
cout<<"test C++ input:"<<endl;for(auto i:nums) {cout<<i<< " " ;}cout<<endl;
}
/*在终端的一行中输入非固定数目的整型数字,并存到数组中,中间以空格分隔*/
//示例: //
//1 2 3 //
/* */
void intFun2(){
vector<int> nums;
int num;
while(cin>>num){
nums.push_back(num);
if(getchar()== '\n')//或者 if(cin.get()== '\n')
break;
}
/*test*/
cout<<"test C++ input:"<<endl;for(auto i:nums) {cout<<i<< " " ;}cout<<endl;
}
/*在终端的一行中输入固定数目的整型数字,并存到数组中,中间以其他字符分隔 */
//示例: //
//3 //
//1,2,3 //
/* */
void intFun3(){
int n;
cin>>n;
vector<int> nums(n);
char sep;
for(int i=0;i<n-1;++i){
cin>>nums[i]>>sep;
}
cin>>nums[n-1];
/*test*/
cout<<"test C++ input:"<<endl;for(auto i:nums) {cout<<i<< " " ;}cout<<endl;
}
/*第一行中输入数字m n,表示后面有n行数据 每行最多m个数据间以空格分隔 */
//示例: //
//3 2 //
//1 6 //
//4 8 //
//2 //
/* */
void intFun4(){
int n,m;
cin>>n;
cin>>m;
vector<vector<int>> nums(n,vector<int>(m,0));
char sep;
for(int i=0;i<n;++i){
for(int j=0;j<m;j++){
cin>>nums[i][j];
}
}
/*test*/
cout<<"test C++ input:"<<endl;
for(int i=0;i<n;++i){
for(int j=0;j<m;j++){
cout<<nums[i][j]<<" ";
}
cout<<endl;
}
}
3.2字符串型输入
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
void strFun1();
void strFun2();
int main(){
strFun2();
return 0;
}
/*给定一行字符串,每个字符串用空格间隔,一个样例为一行 */
//示例: //
//pi ka qiu //
//pi ka qiu //
/* */
void strFun1(){
string str;
vector<string> strs;
while(cin >> str){
strs.push_back(str);
if(getchar()=='\n'){
for(auto & str:strs){
cout<<" "<<str;
}
cout<<endl;
strs.clear();
}
}
}
//
//输入:多个测试用例,每个测试用例一行。每行通过,隔开,有n个字符
//输出:对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格
/*示例:
输入:a,c,bb
f,dddd
nowcoder
输出:
a,bb,c
dddd,f
nowcoder
*/
#include <sstream>
void strFun2(){
string input;
while(getline(cin,input)){
string s;
vector<string> rowStr;
stringstream ss(input);
while(getline(ss,s,',')){
rowStr.push_back(s);
}
sort(rowStr.begin(),rowStr.end());
for(int i=0;i<rowStr.size();i++){
cout<<rowStr[i];
if(i!=rowStr.size()-1){
cout<<",";
}
}
cout<<endl;
}
}
4.练习
实践检验真知,去试一下吧。
牛客网: OJ在线编程常见输入输出练习场