c++速成笔记

可变数组

#include <iostream>//读写
#include <vector>
using namespace std;

int main()
{//可变数组
vector <int> vNums(2);//关键1
vNums[0]=11;
vNums[1]=2;
vNums.push_back(3);//关键2
cout<<"Vector Size:"<<vNums.size()<<endl;
//输出数组长度
    return 0;
}

可变字符串,字符串输出,赋值

#include <iostream>//读写
#include <string>//字符串
#include <vector>
#include <sstream>
//#include<bits/stdc++.h>
using namespace std;


int main()
{
    vector <string> words;
    stringstream ss ("Some Random Words");
    string word;
    while(getline(ss,word ,' '))
    //ss复制到word上 遇到空格暂停
    {
        words.push_back(word);//复制到尾部后面
    }

    for(int i=0;i<word.size();i++)
    {
        cout<< words[i]<<endl;
    }

    return 0;
}

//创建随机数

srand(time(NULL));

字符串

#include <iostream>//读写
#include <string>//字符串
#include <vector>
#include <sstream>
#include<bits/stdc++.h>
using namespace std;


int main()
{
    string str1 = "I,m a string";
    cout << "let : "<< str1[0] << endl;

    //  cout << "Last : " << str1.back() << endl;
    //
    cout << "Length : "<< str1.length() << endl;
//获取字符串长度
    string str2 = str1;
//完全复制
    string str3(str2, 4);
//复制str2前 4 位 到str3

    string str4 = str1 + " and your not ";
//字面意思
    str4.append("!");
//在str4后面加上 !
    cout <<"New String :" <<str4 << endl;
//输出
    str4.erase(12,str4.length() - 1);
//没听懂,关于删除的
    cout <<"New String : " <<str4 << endl;
//
    if(str4.find("string")!=string::npos)
        //find str4中有没有string 并且返回位置(数字)
        //npos字符串的最后一个字符位置
        cout<<"String Index:"<<
            str4.find ("string")<<endl;
//输出位置(数字)
    cout << "Substring : "<<str4.substr(6,7) <<endl;
//获取字串,从第六个字符,长度是7
    string strNum = to_string(1+2);
//将整数转化为字符串
    cout << "I,m a String : " <<strNum<<endl;

    char letterZ  = 'z';
    char num5 = '5';
    char

    return 0;
}

数学函数

在这里插入图片描述
在这里插入图片描述

引用运算符号

共享地址
指针只是存储了地址

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include<bits/stdc++.h>
using namespace std;

void AssingnAge(int *age)
{
    *age=18;
}

int main()
{
    int age = 43;
    AssingnAge(&age);
    cout <<age<<endl;

    return 0;
}


函数宏

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include<bits/stdc++.h>
using namespace std;

#define PI 3.1415926
#define AREA_CIRCLE(radius) (PI * pow(radius , 2))
int main()
{
   cout << "Circle Area"<<
             AREA_CIRCLE(5)<<endl;

    return 0;
}


模板函数

template <typename T>

容器

双端队列

一个两端膨胀或收缩的动态数组

#include<bits/stdc++.h>//万能头
#include <deque>//双端队列头
#include <iterator>//遍历头


using namespace std;
int main()
{
  deque<int >nums={1,2,3,4};
  nums.push_front(0);//在头部添加0
  nums.push_back(5);//在尾部添加5
  for(int x:nums) cout << x<<endl;//遍历输出所有元素
     cout<<nums[0]<<endl;//像普通数组一样单独输出
    return 0;
}


迭代器

用于指向容器内存地址

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int > nums= {1,2,3,4};
     vector<int>::iterator itr;
     for(itr = nums.begin();
            itr<nums.end();
            itr++)
     //itr从nums开头遍历到尾
    {
        cout<<*itr<<endl;//注意*
    }
    
    vector<int> ::iterator int2 = nums.begin();
    //定义迭代器,并初始化为在nums开头
    advance(itr2,2);//itr2迭代器每次向后移动2格
    cout<<*itr2<<endl;
    
    auto itr3 = next(itr2,1);
    cout<<*itr3 <<endl;
    
    auto itr4 = prev(itr2,1);//退回一个itr2
    cout<<*itr4<<endl;
    
    vector<int >nums3 ={1,4,5,6};
    vector<int> nums4 ={2,3};
    auto itr5 =nums3.begin();
    advance(itr5,1);
    
    copy(nums4.begin(),nums4.end(),//copy
         insert(nums3,itr5));
         for(int &i:nums3)
            cout<<i<<endl;
    
    return 0;

}

posted @ 2021-08-26 09:39  kingwzun  阅读(98)  评论(0编辑  收藏  举报