一、Hello World
#include<iostream>
using namespace std;
int main(){
cout << "hello" <<endl;
return 0;
}
编译
g++ helloworld.cpp -o helloworld
执行
./helloworld
二、字符串
- 字符串拼接
- 字符串打印
#include<iostream>
#include<string>
using namespace std;
int main(){
string name = "fzj";
string school = "zju";
cout << name + "\n" + school << endl;
return 0;
}
三、输入输出
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
cin >> s;
cout << s <<endl;
return 0;
}
四、集合
- 创建集合
- 打印集合
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v1;
int word;
while(cin >> word){
v1.push_back(word);
}
for(vector<int>::size_type ix = 0;ix != v1.size();++ix){
cout << v1[ix] << endl;
}
}
输入EOF
,终止输入
五、函数
- 函数调用
- 函数重载
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string getString(string str){
return str;
}
string getString(string str,string str2){
return str + str2;
}
int main(){
string name = "fzj";
cout << getString(name) << endl;
cout << getString(name,"/nzju") << endl;
return 0;
}
网友评论