编程基础 C++版本 C++98 (ISO/IEC 14882:1998):C++的第一个版本于 1998年10月发布。有一些较老的oj在使用。 C++03 (ISO/IEC 14882:2003):在这个版本的c++中,值初始化(int i=1;
)是在2003年2月引入的。C++11(蓝桥杯选用)
:它于 2011年8月发布。这个 C++修订版引入了 Lambda 表达式、委托构造函数、统一初始化语法、自动类型推导auto、nullptr、decltype、Rvalue、References 等。 C++14:它于 2014年8月发布。此版本中添加的功能包括多态lambda、数字分隔符、通用lambda捕获、变量模板、二进制整数文字、引用字符串等。 C++17:它于 2017年 12月发布。它引入了折叠表达式、十六进制浮点文字、u8字符文字、带有初始化程序的选择语句、内联变量等。 C++20:它于 2020年12月发布。包括的一些新功能包括:测试宏3路比较、运算符<=>和运算符==()=默认值、新属性:[[no_unique_address]]、[[likely]]
基本数据类型 int
整数lomg long
长整float
单精度浮点型(小数)double
双精度浮点型(小数)char
字符chan[]
字符串(数组)bool
布尔型(true/false),输出是1/0。
常量 #define
定义常量,如#define PI 3.14
,使用时PI
就是3.14
。const
定义常量,如const int PI = 3.14
,使用时PI
就是3.14
。
变量 int a = 1;
定义变量a,并赋值为1。int a;
定义变量a,不赋值。int a, b, c;
定义多个变量。int a[n];
定义数组,n为数组长度。,元素下标为a[0]~a[n-1]。(注意,n必须为常量(不会变化的))。
C++代码格式与语法基础 Hello, World! 1 2 3 4 5 6 7 8 #include <bits/stdc++.h> using namespace std;int main () { cout<<"Hello, World!" ; printf ("Hello, World!" ); return 0 ; }
输出1 2 Hello, World! Hello, World!
基本数据类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int main () { int x=3 ; double d=3.14 ; char ch ='A' char s[]="Hello" ; bool b=true ; cout<<x<<endl; cout<<d<<'\n' ; cout<<ch<<'\n' ; cout<<s<<'\n' ; cout<<b<<'\n' ; return 0 ; }
示例代码1(数组) 1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;const int N=1e5 +9 ;int a[N];int main () { return 0 ; }
示例代码2(typedef) 1 2 3 4 5 6 7 8 9 10 11 #include <bits/stdc++.h> using namespace std;typedef long long ll;const int N=1e5 +9 ;ll a[N]; int main () { return 0 ; }
示例代码3(字符串) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;int main () { char s[]="Hello" for (int i=0 ;i<4 ;++i) { cout<<s[i]; } cout<<'\n' ; cout<<s<<'\n' ; return 0 ; }
示例代码4(交换变量) 1 2 3 4 5 6 7 8 9 10 11 12 #include <bits/stdc++.h> using namespace std;int main () { int a=5 ,b=3 ; int tmp=b; b=a; a=tmp; cout<<a<<' ' <<b<<'\n' ; return 0 ; }
示例代码5(判断) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <bits/stdc++.h> using namespace std;int main () { int n=10 ; for (int i=1 ;i<=n;++i) { if (i%2 ==0 )cout<<i<<'\n' ; } return 0 ; }
输入输出 scnanf和printf scanf和printf的优势: 1.格式化和输出 2.效率高1 2 3 4 5 6 7 int main () { int a, b; scanf ("%d %d" , &a, &b); printf ("%d, %d\n" , a, b); return 0 ; }
1 2 3 4 5 6 7 int main () { double a, b; scanf ("%lf %lf" , &a, &b); printf ("%.2f, %.3f\n" , a, b); return 0 ; }
1 2 3 4 5 6 7 int main () { char a, b; scanf ("%c %c" , &a, &b); printf ("%c, %c\n" , a, b); return 0 ; }
1 2 3 4 5 6 7 int main () { char s[100 ]; scanf ("%s" , s); printf ("%s\n" , s); return 0 ; }
1 2 3 4 5 6 int main () { char s[15 ]; scanf ("%[^\n]" , s); printf ("%s\n" , s); return 0 ; }
常见类型 int %d
或%i
(少用) double %lf
char %c
char[] %s
string %s
long long %lld
cin 和 cout 1 2 3 4 5 6 7 8 9 10 int main () { int a, b; cin >> a >> b; cout << a << " " << b << endl; double c,d; cin>>c>>d; cout<<c<<d; return 0 ; }
1 2 3 4 5 6 7 int main () { double a, b; cin >> a >> b; cout << fixed << setprecision (2 ) << a << ", " << fixed << setprecision (3 ) << b << endl; return 0 ; }
1 2 3 4 5 int main () { char ch; cin >> ch; cout << ch << endl; }
1 2 3 4 5 int main () { char s[10 ]; cin >> s; cout<< s << endl; }
1 2 3 4 5 int main () { string s; getline (cin, s); cout<< s << endl; }
取消同步流 由于cin和cout需要自动判断变量类型等内部原因flash(),读写效率比scanf和printf更低。当数据量较大时,可能导致程序运行超时,我们可以通过取消同步流来加速cin和cout,加速后效率相差无几。注意,最好不要cin/cout和scanf/printf混用,防止出问题
1 2 3 4 5 6 7 8 int main () { ios::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 ); int x;cin>>x; cout<<x<<'\n' ; return 0 ; }
代码示例1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;using ll = long long ;const int N = 150 ;int a[N];int main () { ios::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 ); int n;cin>>n; for (int i=0 ;i<n;i++) cin>>a[i]; for (int i=0 ;i<n;i++) cout<<a[i]<<' ' ; return 0 ; }
代码示例2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;using ll = long long ;const int N = 150 ;int a[N];int main () { ios::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 ); cin>>s+1 ; for (int i=1 ;s[i];++i) cout<<s[i]; return 0 ; }
代码示例3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;using ll = long long ;const int N = 150 ;char s[N];int main () { scanf ("%s" ,s); for (int i=0 ;s[i];++i) cout<<s[i]<<'\n' ; return 0 ; }
代码示例4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <bits/stdc++.h> using namespace std;using ll = long long ;const int N = 150 ;double a[N];int main () { int n;scanf ("%d" ,&n); for (int i=1 ;i<=n;i++) scanf ("%lf" ,&a[i]); double sum=0 ; for (int i=1 ;i<=n;i++) sum+=a[i]; printf ("%.2lf" ,sum); return 0 ; }
示例题目 题目详见蓝桥OJ:A+B问题 在里面好好练习输入输出吧,顺便练习一下取消同步流。
String的使用 string简介 string是C++标准库的重要组成部分,主要用于字符串处理。 使用string库需要在头文件中包括该库#include string与char[]不同,string实现了高度的封装,可以很方便地完成各种字符串的操作,比如拼接、截取、匹配等等。
字符串管理:string封装了字符串的存储和管理。它自动处理字符串的内存分配和释放,避免了手动管理内存的麻烦。
动态大小调整:string可以根据需要自动调整字符串的大小。在添加或删除字符时,string会自动调整内部的存储容量,确保足够的空间来容纳字符串。
安全性:string提供了一些方法来确保字符串的安全性。例如,它提供了越界访问检查,以避免访问超出字符串范围的字符。
迭代器支持:string支持迭代器,可以使用迭代器遍历字符串中的字符,进行字符级别的操作。
兼容性:string是C++标准库的一部分,因此在C++中广泛使用,并且与其他标准库组件和C++语言特性兼容。string的声明与初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <iostream> #include <string> int main () { std::string str1; std::string str2 = "Hello, World!" ; std::string str3=str2; std::string str4 = str2.substr (0 , 5 );; const char * charArray="Hello" ; std::string str5 (charArrary) ; std::string str6 (5 , 'A' ) ; std::cout << str1 << std::endl; std::cout << str2 << std::endl; std::cout << str3 << std::endl; std::cout << str4 << std::endl; std::cout << str5 << std::endl; std::cout << str6 << std::endl; return 0 ; }
输出1 2 3 4 5 6 7 Hello, World! Hello, World! Hello Hello AAAAA
string的常用操作0:printf输出字符串 1 2 3 4 5 6 7 8 9 10 11 #include <bits/stdc++.h> using namespace std;int main () { ios::sync_with_stdio (0 ),cin.tie (0 ),cout.tie (0 ); char buf[100 ]; scanf ("%s" ,buf); string str (buf) ; printf ("str = %s\n" ,str.c_str ()); return 0 ; }
string的常用操作1:获取字符串长度 1 2 3 std::string str = "Hello, World!" ; int len = str.length ();std::cout << len << std::endl;
string的常用操作2:字符串拼接 1 2 3 4 5 6 std::string str1 = "Hello, " ; std::string str2 = "World!" ; std::string str3 = str1 + str2; std::string str4 = str1.append ("," ).append (str2); std::cout << str3 << std::endl; std::cout << str4 << std::endl;
string的常用操作3:字符串查找 1 2 3 4 5 6 7 std::string str = "Hello, World!" ; size_t pos = str.find ("World" );if (pos != std::string::npos) { std::cout << "Found at position: " << pos << std::endl; } else { std::cout << "Not found" << std::endl; }
string的常用操作4:字符串替换 1 2 3 std::string str = "Hello, World!" ; str.replace (7 , 5 , "Universe" ); std::cout << str << std::endl;
string的常用操作5:字符串截取 1 2 3 std::string str = "Hello, World!" ; std::string subStr = str.substr (7 , 5 ); std::cout << subStr << std::endl;
string的常用操作6:字符串比较 string重载了不等号,所以可以直接使用s1 < s2的方式来比较string的大小,比较的规则是按照字典序大小进行比较。 字典序的比较方法是从小到大一个一个比较,一旦遇到不相等的字符就确定大小关系。 例如: аааа < bььь аzz < baaa aZZZZZZZZZZZZz < b lanqiao == lanqiao1 2 3 4 5 6 7 8 9 10 std::string str1 = "Hello" ; std::string str2 = "World" ; int result = str1.compare (str2);if (result == 0 ) { std::cout << str1 << " is equal to " << str2 << std::endl; } else if (result < 0 ) { std::cout << str1 << " is less than " << str2 << std::endl; } else { std::cout << str1 << " is less than " << str2 << std::endl; }
string的常用操作7:字符串遍历 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 string s = "Hello" ; for (int i = 0 ; i < s.length (); ++i)cout << s[i];cout<<'\n' ; for (auto i : s){ cout << i; i='z' ; } cout<<'\n' ; for (auto &i : s){ cout<<i; i='a' ; } cout<<'\n' ; cout<<s<<'\n' ;
1 2 3 4 string s = "Hello" ; for (auto it = s.begin (); it != s.end (); it++) { cout << *it << endl; }
string练习题:反转字符串中的字符 题目详见蓝桥OJ:反转字符串中的字符 1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;int main () { string s; getline (cin, s); for ( int i=s.size ()-1 ; i>=0 ; i-- ) cout << s[i]; return 0 ; }
1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;int main () { string s; getline (cin, s); reverse (s.begin (), s.end ()); cout << s; return 0 ; }
1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;int main () { string s; getline (cin, s); for ( int i=0 ; i<s.size ()/2 ; i++) swap (s[i], s[s.size ()-1 -i]); cout << s; return 0 ; }
小作业:妮妮的翻转游戏 题目详见蓝桥OJ:妮妮的翻转游戏 方法很多,这里列举一种。1 2 3 4 5 6 7 8 9 10 11 #include <bits/stdc++.h> using namespace std;int main () { ios::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 ); int x; cin>>x; if (x)x=0 ; else x=1 ; cout<<x<<'\n' ; return 0 ; }