LeetCode刷题知识点总结(更新ING)

                                                     一、经验分享


待更............


                                                    二、知识点总结


1、 求一个vector定义的向量的长度(size()函数

       例:vector<int> nums

       长度:int size = nums.size()     

              

2、不写排序代码对数组进行简单的升序排序(sort()函数

       例:vector<int> nums;   假设nums里面的元素都是数字

       排序:sort(nums.begin(),nums.end())     排序后的结果是原数组的升序排序

3、breakcontinuegoto语句的使用

  • break 

      作用:用于终止当前循环,且只能终止距离它最近的循环。

  • continue

      作用:用于跳过本次循环余下的语句,转去判断是否需要执行下次循环,也就是对于最近的一层循环来说(for、while、                         do......while)

  •    goto

      作用:goto语句允许把控制无条件转移到同一函数内的被标记的语句。(但是尽量不要使用goto语句,因为它使得程序的控制流难以跟踪,使程序难以理解和难以修改)

     格式:goto label;              //直接跳转到label处,执行label冒号后的语句

                ......

                ......

               label:

                       statement;                   

  4、使用vector定义一维、二维向量

  •   一维向量

        1)vector<int> nums;                    //创建一个向量存储容器 int

        2)vector<int> nums(10);             //定义10个整型元素的向量,其值是不确定的

        3)vector<int> nums(10,1);          //定义10个整型元素的向量,其每个元素的初值为1

        4)vector<int> nums(a);              //用向量a来创建nums向量,整体复制性赋值

        5)vector<int> nums(a.begin(),a.begin+3)   //定义nums中的值为a中的第0个到第2个元素

       6)int a[7]={1,2,3,4,5,6,9,8};

           vector<int> nums(a,a+7);         //从数组中获得初值

  • 二维向量

       1)vector<vector<int>> nums;       //创建一个二维向量容器 int

       2)vector<vector<int>> nums(3, vector<int>(4)) ;    //二维向量nums,它含有三个元素,每个元素含有4个int型的向量

5、针对vector对象的一些操作函数

  • push_back()

      作用:在vector定义的向量后插入一个元素。

                 vector<int> nums;

                 nums.push_back(5);     

  • pop_back()

      作用:在vector定义的向量中,删除最后一个元素。

                 vector<int> nums;

                 nums.pop_back();

6、将char类型的字母转换成string类型的

      vector<string> A1;

      char nums = 'a' +3;

      stringstream stream;

      stream<<nums; 

     A1.push_back(stream.str());            //A1中存的就是string类型的字母         

             A1中的结果是:["d"]


猜你喜欢

转载自blog.csdn.net/gaoyu1253401563/article/details/89107286
今日推荐