string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main(void){
string arr = "hello world";
string s(5,'a');
string s1("three")

cout << arr <<endl;
cout << s <<endl;
cout << s1 <<endl;

}
1
2
3
4
string a = "hello";
a = "pre" + a + "world";

cout << a <<endl;
1
2
3
string str;
getline(cin,str);
cout << str.empty() <<endl;
1
2
3
str.replace(2,3,"ABCD",2,2);
str.erase(3,1);

1
2
3
4
5
str.c_str();
#turn string into a const string
char* p = (char*)str.c_str();


1
2
3
4
5
6
7
8
9
10
11
12
13
istringstream is(src);

string s1;
int a;
double b;
char c;

is >> s1 >> a >> b >> c;


ostringstream os;
os << s1 <<" "<< a << " " << b << " " <<c;

foreach &

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int arr[] = {1,2,3,4,5};

for(int x:arr){
cout<<x<<endl;
}


int a = 2;
int& b = a;
b = 4;
cout<<a<<endl;

for(int &x:arr){
x = 1;
}
for(int x:arr){
cout<<x<<endl;
}


static

1
2
3
4
5
6
7
8
9
10
11
void f(){
static int num = 0;
num++;
cout << num << endl;
}
int main(){

f();
f();

}

默认参数

1

模板化,泛型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

template<class T>
bool maxA(T a,T b){
return a > b;
}

int main() {

cout << maxA(10,20) << endl;
cout << maxA('a','b') << endl;
cout << maxA<int>(20.6,10) << endl;
}

vector

1
2