初涉演算法——C++

來源:互聯網
上載者:User

標籤:c++   聲明變數   return   class   題目   code   聲明   演算法   初始化   

一、sstream標頭檔運用

題目:輸入資料的每行包括若干個(至少一個)以空格隔開的整數,輸出每行中所有整數之和。

#include<iostream>#include<cstring>#include<sstream>using namespace std;int main(){    string line;    while(getline(cin,line)){        int sum=0, x;        stringstream ss(line);       //建立一個“字串流”——ss        while(ss>>x) sum+=x;    //像讀取cin那樣讀取ss        cout<<sum<<endl;    }}

二、再談結構體struct

新的定義方式:

struct Point{    //code}; 

特點:在struct內可以有成員函數。

struct Point{    int x,y;    Point(int x=0,int y=0):x(x),y(y) {}    //預設參數,即Point()相當於Point(0,0)。     //函數Point無傳回值,稱為建構函式,其是在聲明變數時調用的。    //“:x(x),y(y)”是一個簡單的寫法,表示把成員變數x初始化為參數x,成員變數y初始化為參數y。 }; 

與C++結合:可以重載運算子,為結構體定義“加法”,為結構體定義流輸出方式。

struct Point{    int x,y;    Point(int x=0,int y=0):x(x),y(y) {}    //也可寫成Point(int x=0,int y=0){this->x=x;this->y=y;} }; Point operator + (const Point& A, const Point& B){    return Point(A.x+B.x,A.y+B.y);}ostream& operator<<(ostream &out, const Point& p){    out<<"("<<p.x<<","<<p.y<<")";    return out;}

應用舉例:

 1 #include<iostream> 2 using namespace std; 3 struct Point{ 4     int x,y; 5     Point(int x=0,int y=0):x(x),y(y) {}     6 };  7 Point operator + (const Point& A, const Point& B){ 8     return Point(A.x+B.x,A.y+B.y); 9 }10 ostream& operator<<(ostream &out, const Point& p){11     out<<"("<<p.x<<","<<p.y<<")";12     return out;13 }14 int main()15 {16     Point a, b(1,2);17     a.x = 3;18     cout<< a+b <<endl;        //輸出(4,2) 19 }

三、模板

沒有模板的求和:只能求一種資料類型數組的和。

1 int sum(int* begin, int* end)2 {3     int *p = begin;4     int ans = 0;5     for(int *p = begin; p != end; p++)6         ans += *p;7     return ans;8 }

使用模板:下述函數可以給double數組和Point數組求和。

1 template<typename T>2 T sum(T* begin, T* end)3 {4     T *p = begin;5     T ans = 0;6     for(T *p = begin; p != end; p++)7         ans = ans + *p;            //Point結構體中沒有定義"+="運算子。 8     return ans;9 }
1 int main()2 {3     double a[] = {1.1, 2.2, 3.3, 4.4};4     cout<<sum(a,a+4)<<endl;5     Point b[] = {Point(1,2), Point(3,4), Point(5,6), Point(7,8)};    //結構體數組 6     cout<<sum(b,b+4)<<endl;7 }

帶模板的結構體:將上述的結構體Point編程模板。

 1 template <typename T>  2 struct Point{ 3     T x,y; 4     Point(T x=0,T y=0):x(x),y(y) {}     5 };  6 template <typename T>  7 Point<T> operator + (const Point<T>& A, const Point<T>& B){ 8     return Point<T>(A.x+B.x,A.y+B.y); 9 }10 template <typename T> 11 ostream& operator << (ostream &out, const Point<T>& p){12     out<<"("<<p.x<<","<<p.y<<")";13     return out;14 }
1 int main()2 {3     Point<int> a(1,2), b(3,4);4     Point<double> c(1.1,2.2), d(3.3,4.4);5     cout<<a+b<<" "<<c+d<<endl;       //輸出(4,6) (4.4,6.6) 6 }

初涉演算法——C++

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.