【学习笔记】C to C++ | C++ primer笔记

  • 文档说明:C to C++ 学习笔记自用。

Start with the class

class Name{
   
  Data, 
  Operation/Method,
}

class PermutationGroup{
   
public:
    int pN, gN; // data element type
    Permutation gp[gNMax]; //data element type
    PermutationGroup(int n); //operation; constructor of the class: same name with the class
    

    Permutation Inv(Permutation p);
    Permutation Mul(Permutation p, Permutation q);
    void PrintPermutation(Permutation p);
    void PrintPG(); // Print the permutation group
    void PrintDerivedPG(int dn);  // Print the dn-th derived permutation group
};

Main file

int main(int argn, char* argc[])
{
   
    int n, dn;
    if (argn<2){
    n = 4; dn = 3; }
    else if (argn<3){
    n = std::stoi(argc[1]); dn = 3; }
    else{
    n = std::stoi(argc[1]); dn = std::stoi(argc[2]); }

    PermutationGroup g(n);
    printf("Permutation group G[%d] : totally %d permutations\n", g.pN, g.gN);
    g.PrintPG();
    g.PrintDerivedPG(dn);

    /*
    Permutation p; p.n = 4; p.p[0] = 2; p.p[1] = 3; p.p[2] = 4; p.p[3] = 1;
    g.PrintPermutation(p);
    g.PrintPermutation(g.Mul(p,p));
    g.PrintPermutation(g.Inv(p));
    g.PrintPermutation(g.Mul(p,g.Inv(p)));
    */

    return 0;
}

不带virtual:执行原来的意义
带virtual:

C++的条件编译

#if 0
#endif

#if 1
#else
#endif

#if 1
#elif
#elif
#endif

#ifdefXXX   如果已经定义了这个宏
#endif

#ifndefXXX
#else
#endif

C++标准输入输出流-名字空间

cout是标准名字空间std的一个名字,使用的时候要加上名字空间限定std::

例如std::cout

end of file in Windows: ctrl+z

文件的写入,文件的读取和输出

# include <iostream>
# include <fstream>
# include <string>

using namespace std;

int main(){
   
    ofstream oF("test.txt");
    oF<<3.14<<"hello world\n";
    oF.close();//关闭内容

    ifstream iF("test.txt"); //写入文件,把文件load进来
    double d;
    string str;

    iF >>d>>str;
    cout << d << " " << str<<endl;

    return 0;
}

引用变量

引用变量是其他变量的别名,一旦引用了一个变量,就不能引用其他变量

int &r = a; //r 是a的引用变量,也就是说,r和a是一样的

引用变量和被引用变量必须同一类型;对引用变量和原变量的操作是一样的

函数的值形参

函数改变原函数:需要输入变量地址

void swap(int *x, int *y){
   
  int t = *x;
  *x = *y;
  *y = t;
}

函数的默认形参&函数重载

形参又默认值

默认形参必须在非默认形参右边

void print(char ch, int n=1)

允许同一作用域里面有同名的函数,形参不同即可

int add(int x, int y);
double add(double x, double y);
(double)5 //强制类型转换

函数模板

为了避免修改起来麻烦,使用函数模板,将如同double, int等的数据类型变成类型模板参数

template <typename T>//表示类型模板参数
T add(T x, T y){
   
  return x+y;
}

使用方法:

cout << add<int>(5,3) <<endl;
cout << add<double>(5.3,7.8) <<endl;

如果里面是int,则生成数据类型T=int的函数
字符串相加就是两个字符出链接

C++primer - page 680

  • Templates are foundation for generic programming in C++
  • If the function body is the same, only the type of input or output are different, we shall use function template
template <typename T>
int compare(const T &v1, const T &v2)
{
   
  if (v1<v2) return -1;
  if (v1>v2) return 1;
  return 0;
}

//usage:
compare(1,0)// T as int

vector<int> vec1{
   1,2,3}, vec2{
   4,5,6};
compare(vec1, vec2)

In the template definition, each type parameter must be preceeded by the keyword class or typename.

template <typename T, class U> calc
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值