C++(一)

2018-12-18  本文已影响0人  午后凉白开

一、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;

}

上一篇下一篇

猜你喜欢

热点阅读