美文网首页
C++(一)

C++(一)

作者: 午后凉白开 | 来源:发表于2018-12-18 17:31 被阅读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;
    
    }
    
    

    相关文章

      网友评论

          本文标题:C++(一)

          本文链接:https://www.haomeiwen.com/subject/dxwlkqtx.html