#include<iostream>#include<vector>usingnamespace std;/*
为减少占用的内存,可不使用前面介绍的静态数组,而使用动态数组,并在运行阶段根据需要增大动态数组。
C++提供了 std::vector,这是一种方便且易于使用的动态数组
避免超越数组边界至关重要,这被称为缓冲区溢出。将输入用作索引前应对其进行检查,这有助于避免跨越数组边界
动态数组让程序员无需在编译阶段考虑其最大长度,使用动态数组可更好地管理内存,以免分配过多的内存,而又不使用它们。
*/intmain(){// 声明一个动态数组,初始大小为3
vector<int>dynamic_array(3);
dynamic_array[0]=1;
dynamic_array[1]=2;
dynamic_array[2]=3;
cout <<"Number of integers in the array: "<< dynamic_array.size()<< endl;
cout <<"Enter another number of the array: "<< endl;int another_number =0;
cin >> another_number;
dynamic_array.push_back(another_number);//push_back()函数将新元素添加到动态数组的末尾
cout <<"Number of integers in the array: "<< dynamic_array.size()<< endl;// 索引从零开始,而 size()返回矢量包含的元素数,因此最后一个元素的索引为size()-1。
cout <<"The last number in the array: "<< dynamic_array[dynamic_array.size()-1]<< endl;return0;}
C++字符串
#include<iostream>#include<string>usingnamespace std;/*
C 风格字符串是特殊的 char 数组,用终止空字符‘\0’标识末尾。
更重要的是,您了解到,C++提供了更佳的选择——std::string,它包含一些方便的函数,让您能够判断字符串的长度、拼接字符串等
*/intmain(){
string greeting("Hello, world!");// 初始化字符串
cout << greeting << endl;
cout <<"Enter a line of text: "<< endl;
string first_line;getline(cin, first_line);// 读取一行文本
cout <<"Enter another line of text: "<< endl;
string second_line;getline(cin, second_line);// 读取另一行文本
cout <<"Result of concatenation: "<< endl;
string concat = first_line +" "+ second_line;
cout << concat << endl;// 输出拼接后的字符串
cout <<"Copy of concatenated string: "<< endl;
string copy = concat;
cout << copy << endl;// 输出拼接后的字符串的副本
cout <<"Length of concat string: "<< concat.length()<< endl;// 输出拼接后的字符串的长度return0;}
前缀运算符和后缀运算符
#include<iostream>usingnamespace std;intmain(){int my_int =101;
cout <<"Start value of integer being operated: "<< my_int << endl;int post_fix_increase = my_int++;
cout <<"Result of Postfix increment: "<< post_fix_increase << endl;
cout <<"After Postfix increment, my_int is: "<< my_int << endl;
cout << endl;
my_int =101;// reset the value of my_intint pre_fix_increase =++my_int;
cout <<"Result of Prefix increment: "<< pre_fix_increase << endl;
cout <<"After Prefix increment, my_int is: "<< my_int << endl;
cout << endl;
my_int =101;// reset the value of my_intint post_fix_decrement = my_int--;
cout <<"Result of Postfix decrement: "<< my_int--<< endl;
cout <<"After Postfix decrement, my_int is: "<< my_int << endl;
cout << endl;
my_int =101;// reset the value of my_intint pre_fix_decrement =--my_int;
cout <<"Result of Prefix decrement: "<< pre_fix_decrement << endl;
cout <<"After Prefix decrement, my_int is: "<< my_int << endl;return0;}
逻辑运算符
/*
not !
and &&
or ||
exclusive or ^
*//*
在if语句中使用逻辑NOT和AND运算符(!和&&)进行条件处理
*/#include<iostream>usingnamespace std;intmain(){
cout <<"Use boolean values (0 / 1) to answer the questions"<< endl;
cout <<"Is it raining?";bool raining =false;
cin >> raining;
cout <<"Do you have buses on the streets?";bool buses =false;
cin >> buses;// Conditional statement uses logical AND and NOTif(raining &&!buses)
cout <<"You cannot go to work!"<< endl;else
cout <<"You can go to work!"<< endl;if((!raining)&& buses)
cout <<"Enjoy the sun and have a day!"<< endl;return0;}
/*使用逻辑运算符NOT和OR帮助判断您能否购买那辆梦寐以求的汽车
下面内容,没有写对应得程序
对整数的各位执行NOT、AND、OR和XOR运算,以演示按位运算符的用法
移位运算符将整个位序列向左或向右移动,其用途之一是将数据乘以或除以2n。
<< 1 乘以 2
>> 1 除以 2
符合运算符
+= -= /= %=
*/#include<iostream>usingnamespace std;intmain(){
cout <<"Answer questions with 0 or 1"<< endl;
cout <<"Is there a deep discount on your favorite car? ";bool discount =false;
cin >> discount;
cout <<"Did you get a fantastic bonus?";bool fantastic_bonus =false;
cin >> fantastic_bonus;if(discount || fantastic_bonus)
cout <<"Congratulations, you can buy your dream car!"<< endl;else
cout <<"Sorry, you can't buy your dream car."<< endl;return0;}
控制程序流程
/*
if..else
function_1 : 将字符串复制到char数组中之前,检查数组的容量
嵌套if
if {
if {}...
}
swith-case: switch-case结构非常适合与枚举常量结合使用。
switch(expression){
case constant1:
statement1;
case constant2:
statement2;
default: statementN;
}
运算符 ?: --- 三目运算符
int max = (a > b ? a : b); max为a, b中大的那个
goto语句
some_function(){
jump_to_point: //called a label
code....
goto jump_to_point;
}
while循环
while (expression){
// 只要expression为true,就将执行该语句块。因此,必须确保expression在特定条件下将为false,否则while循环将永不停止。
// condition evaluates to true
statement_block;
}
do-while循环:来确保语句至少执行一次。
do{
statement_block;
}while(condition);
for循环:
在for循环的初始化表达式中,可初始化多个变量。
for (initialization; condition; increment){
statement_block;
}
使用continue 和 break修改循环的行为
嵌套循环遍历多维数组
function_2:使用嵌套循环遍历二维int数组的元素
function_3: 使用嵌套循环计算斐波纳契数列
*/#include<iostream>#include<string>usingnamespace std;// void function_1(){// char Buffer[20] = {'\0'};// cout << "Enter a line of text: " << endl;// string line_entered;// getline(cin, line_entered);// if (line_entered.length() < 20){// //现在好像运行不了了// strcpy(Buffer, line_entered.c_str()); //c_str()将string转换为C风格字符串// cout << "Buffer contains: " << Buffer << endl;// }// }voidfunction_2(){constint MAX_ROWS =3;constint MAX_COLS =4;// 2D array of integersint my_int[MAX_ROWS][MAX_COLS]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};for(int row =0; row < MAX_ROWS; row++){for(int col =0; col < MAX_COLS; col++)
cout <<"my_int["<< row <<"]["<< col <<"] = "<< my_int[row][col]<< endl;}}voidfunction_3(){constint NUMBERS_TO_CALCULATE =3;
cout <<"This program will calculate "<< NUMBERS_TO_CALCULATE <<" Fibonacci numbers."<< endl;int num1 =0, num2 =1;char want_more ='\0';
cout << num1 <<" "<< num2 <<" ";do{for(int index =0; index < NUMBERS_TO_CALCULATE; index++){
cout << num1 + num2 <<" ";int temp = num2;
num2 = num1 + num2;
num1 = temp;}
cout << endl <<"Do you want to calculate more Fibonacci numbers? (y/n) ";
cin >> want_more;}while(want_more =='y'|| want_more =='Y');
cout <<"Goodbye!"<< endl;}intmain(){// function_1();// function_2();function_3();return0;}