close

1.javascript 中 陣列傳給method是位址,其他則是複製,所以array在method改過後外面的也會變

var c=[5,4,23];
var g=function(x){
  x[0]=100;
}
g(c);
console.log(c);

var d=5;
var f=function(x){
  x=x+1000;
}
f(d);
console.log(d)

//output 

[100, 4, 23]
5

2.c++模板函数

一個方法能給不同的type使用

 

未使用模板

sort(int a ,int b)

sort(double a,double b)

使用後

template <class T>
void sort(T array[]){
    cout<<a[0];
}

////

#include <iostream>
using namespace std;
template <class T>
void print(T a[]){
    cout<<a[0];
}

  int main()
  {
    int a[]{4,2,3};
    double c[]{3,3,2};
    print(c);
    print(a);


  }
 

3.c++ 類模板:產生出一個類模板,成員名稱相同但type可以隨傳入的T改變

template <class T>
class B{
    public:
        T a;

};

  int main()
  {
    B<int> c;
    B<double> d;
    cout<<typeid(c.a).name()<<endl; //int  i
    cout<<typeid(d.a).name()<<endl;  //double  d

  }
 

 

 

4.auto 關鍵字:根據賦值的資料來判斷是什麼type

auto a=5;    a就是int

auto 變數實際的型別是在編譯時期進行判定的,在真正執行時就跟一般的 C++ 型別一樣,所以不會造成額外的負擔。

由於 auto 是根據變數的資料來判斷實際的型別,所以若是僅使用 auto 宣告變數,但未定義變數的內容,就會產生錯誤:

// 錯誤
auto z;
在 C++11 的標準中,auto 不可以用於函數的傳回值,但後來 C++14 的標準加入了這個用法:

// 在 C++11 中無法使用,但 C++14 則可以
auto my_fun() {
  int value = 3;
  return value;
}

int main() {
  my_fun();
  return 0;
}
編譯器會依照函數的傳回值來決定 auto 實際對應的變數型別。

https://blog.gtwang.org/programming/cpp-auto-variable-tutorial/

5.樣版類別的樣版方法用法

template <class T>
class B{
    public:
        T a();
        T c;


};
template <class T>
T B<T>::a(){
return 5;
}
  int main()
  {
    B<int> d;
    cout<<d.a();
    return 0;
  }
6.陣列記憶體 用法

  int main()
  {
    int a[]={1,3,4}; 
    int* b=a+2; // a的第二個位置的adrress給b

    cout<<*b;  //印出b指向記憶體的值   4
    return 0;

7.運算子重載

using namespace std;
class a{
public :
    int b;

    a operator+(const a& p){
        a x;
        x.b=1000;

    return x;
    }
};
  int main()
  {
 a b;
 a c;
 a d;
 d=b+c;
   cout<<d.b;
    return 0;
  }
7.1運算子重載2

class a{
public :
    int b;

    int operator+(const a& p){
      int g=p.b+this->b;

    return g;
    }
};
  int main()
  {
 a b;
 b.b=5;
 a c;
 c.b=100;
 int d;
 d=b+c;
   cout<<d;
    return 0;
  }

8.c++ 傳參考   int method( int   &a),方法參數有&變數直接參考到記憶體

int c(int &f){
    f=f+1000;
    return 5;
}
  int main()
  {
    int a=5;
    c(a);
    cout<<a;
    return 0;
  }
9.c++ lambda用法

#include <iostream>
#include <typeinfo>
#include <vector>
#include <functional>
using namespace std;

  int main()
  {

    int a=5;

    auto l = [=](int v)->int{return a;};

  //  [=]抓取外部所有變數傳值(拷貝進去,不影響外部變數

//[]完全不抓外部變數

//[&]   所有外變數傳參考進來,在lambda裡修改外部變數,外部變數也會跟著變

    cout<<l(11);
  }
9.1 [&]

using namespace std;

  int main()
  {
    int a=5;

    auto l = [&](int v)->int{a=111;return a;};

    cout<<l();//111
    cout<<a;//111
  }
9.2[x,&y]  x傳值y傳參

9.3[=,&y] 除了y用傳絫其他用傳值

9.4mutable可以對外部變數的傳值進行修改


  int main()
  {
    int a=5;

    auto l = [=](int v)mutable->int{a=999;return a;};

    cout<<l(3);//111

  }
10.sstream庫的Stringstream類

可以用來切割字串或int轉成string或string轉int,double ,float

第一個轉完要清空否則第二個沒辦法用因為第一個東西還留在stream

.str()方法可以印出stream裡的東西

.clear()清空stream裡的東西

10.1int轉string

#include <iostream>
#include <sstream>
using namespace std;

  int main()
  {
      int b=5;
      stringstream c;
      c<<b;
string a;
c>>a;
cout<<a;
  }

10.2 string 轉int

#include <iostream>
#include <sstream>
using namespace std;

  int main()
  {
    stringstream a;
    int b;
    string c="55";
    a<<c;
    a>>b;
    cout<<b;
  }
10.3檢查轉型是否成功

  int main()
  {
    stringstream a("f");
    int b;
    a>>b;
    if (!a) cout<<5;
    else cout<<"ok";


  }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 學習程式 的頭像
    學習程式

    程式學習日記,如果我幫助了你請讓我知道

    學習程式 發表在 痞客邦 留言(0) 人氣()