C++ Prepmier Plus丨笔记丨第四章
#include<iostream>
#include<string>
#include<array>
using namespace std;
int main(){
//ARRAY
/*int yam[3];
int patato[3] = {3,4,5};//define it in this way, [] is allow to be empty BUT NOT RECOMMENDED
yam[0] = 0;
yam[1] = 1;
yam[2] = 2;
//cout<<yam<<endl;//output the first address of the array
cout<<yam[0]<<yam[1]<<yam[2]<<endl;
cout<<patato[0]<<patato[1]<<patato[2]<<endl;
cout<<"sizeof(element in yam) = "<<sizeof(yam[0])<<", sizeof(yam) = "<<sizeof(yam)<<endl;
cout<<"So yam contains "<<sizeof(yam)/sizeof(yam[0])<< " of elements."<<endl;
int tomato[5] = {1, 2};
cout<<"define tomato and initial it as:int tomato[5] = {1, 2}"<<endl;
for(int i = 0; i<5; i++){
cout<<"tomato["<<i<<"] is "<<tomato[i]<<endl;
}*/
//CHARACTER STRING
/*char cat[3] = {'1','2','\0'};//when cout this char string, cout will stop when meet the '\0'
cout<<"cout char cat[3] = {'1','2','\\0'} is "<<cat<<endl;
char dog[3] = "hi";//can only put less than 2 character in the {}
cout<<"cout char dog[3] = \"hi\" is "<<dog<<endl;
char iAmString[5] = {'a','b','c','d','\0'};
char iNotString[5] = {'a','b','c','d','e'};
cout<<"char iAmString[5] = {'a','b','c','d','\\0'} will be cout as "<<iAmString<<endl;
cout<<"char iNotString[5] = {'a','b','c','d','e'} will be cout as "<<iNotString<<endl;
//only when it end with '\0', can it be considered as Character String
cout<<"strlen(iAmString) is "<<strlen(iAmString)<<" , not "<<strlen(iAmString) + 1;
cout<<" because strlen() will not count the '\\0'"<<endl;*/
//STRING INPUT
/*char input[15] = {};
cout<<"Enter \"hello WORLD\"";
cin>>input;
cout<<"What \"cin\" can really take in is: \""<<input<<"\""<<endl<<"Because it consider Blankspace( and Tab and Enter) as a stop sign"<<endl;
//cin.ignore();//erase the input buffer//uneffective
cin.getline(input,15);
cout<<"Enter \"hello WORLD\"";
cin.getline(input, 15);
cout<<"What \"cin.getline\" can really take in is: \""<<input<<"\""<<endl;*/
//STRING
/*//to use STRING you need to #include<string>
string str1 = "First section";//QUESTION can't initial with {...}, WHY?
cout<<"str1 is :"<<str1<<endl;
string str2 = "Second section";
cout<<"str2 is :"<<str2<<endl;
cout<<"str1 + str2 = "<<str1+str2<<endl;
cout<<"Input something with blankspace : ";
getline(cin,str1);//This is how you get whole line of input and put it into a STRING
cout<<str1<<endl;*/
//STRUCT
/*struct fruit{
string name;
int volume;
int price;
};
fruit orange = {
"orange",
20,
2
};
cout<<"fruit orange: second letter in orange is: orange.name[1], which is "<<orange.name[1]<<endl;
fruit apple = {};// CAN'T RUN WITHOUT THE "="
cout<<"the name of apple is: \""<<apple.name<<"\""<<endl;
struct vegetable{
int volume;
int price;
}patato = {
13,
3
};
cout<<patato.price<<endl;*/
//UNION
//ENUM
/*enum Response {No, Yes, Maybe};//There are NO = WHEN DEFINE ENUM
cout<<No<<endl;*/
//POINTER
/*int intArray[2] = {1,2};
cout<<"The address of intArray[0] = "<<&intArray[0]<<endl;
cout<<"The address of intArray[1] = "<<&intArray[1] << endl;
int diff =int( &intArray[1]) - int(&intArray[0]);//ATTENTION! want the sub run with meaning of differ byte than differ element , do TYPE CAST
cout<<"They difference for "<<diff<<" and sizeof(int) is "<<sizeof(intArray[0])<<endl;
int var = 5;
int* pVar = &var;
cout<<"int var, then int *pVar = &var";
cout<<"var = "<<var<<endl;
cout<<"&var = "<<&var<<endl;
cout<<"pVar = "<<pVar<<endl;
cout<<"*pVar = "<<*pVar<<endl;
//POINTER and ARRAY
short tell[3];
cout<<"The lengh of tell is : "<<sizeof(tell)/sizeof(tell[0])<<" ; sizeof element in tell is: "<<sizeof(tell[0])<<endl;
cout<<"cout array's name: "<<tell<<" cout array'name + 1: "<<tell+1<<endl;
cout<<"cout array's pointer: "<<&tell<<" cout array's pointer + 1"<<&tell+1<<endl;*/
return 0;
}