一些常用的头文件
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
C++万能头文件,但编译不一定能支持
#include
它的源码
// 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
// .
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if __cplusplus >= 201103L
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
// C++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
(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等方式,要根据具体的使用场景,选择具体的输入输出函数。
#include
#include
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 nums(n);//或者 vector nums;nums.resize(n);
for(int i = 0 ;i>nums[i];
}
/*test*/
cout<<"test C++ input:"<>n;
vector nums;
for(int i = 0 ;i>val;
nums.push_back(val);
}
/*test*/
cout<<"test C++ input:"< nums;
int num;
while(cin>>num){
nums.push_back(num);
if(getchar()== '\n')//或者 if(cin.get()== '\n')
break;
}
/*test*/
cout<<"test C++ input:"<>n;
vector nums(n);
char sep;
for(int i=0;i>nums[i]>>sep;
}
cin>>nums[n-1];
/*test*/
cout<<"test C++ input:"<>n;
cin>>m;
vector> nums(n,vector(m,0));
char sep;
for(int i=0;i>nums[i][j];
}
}
/*test*/
cout<<"test C++ input:"<
#include
#include
#include
#include
using namespace std;
void strFun1();
void strFun2();
int main(){
strFun2();
return 0;
}
/*给定一行字符串,每个字符串用空格间隔,一个样例为一行 */
//示例: //
//pi ka qiu //
//pi ka qiu //
/* */
void strFun1(){
string str;
vector strs;
while(cin >> str){
strs.push_back(str);
if(getchar()=='\n'){
for(auto & str:strs){
cout<<" "<
void strFun2(){
string input;
while(getline(cin,input)){
string s;
vector rowStr;
stringstream ss(input);
while(getline(ss,s,',')){
rowStr.push_back(s);
}
sort(rowStr.begin(),rowStr.end());
for(int i=0;i
实践检验真知,去试一下吧。
牛客网: OJ在线编程常见输入输出练习场