C++ primer review (three, string, vector and array)

3.1, the using statement of the namespace

You can take out a namespace in advance, which makes it more convenient to write programs.
Such as: using

#include <iostream>
#include <windows.h>
using namespace std;//声明std命名空间
int main()
{
    
       
    cout<<"成功"<<endl;
    system("pause");
    return 0;
}

3.2, string standard library

1. Initialization:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std
int main()
{
    
       
    string a;//默认初始化
    string a1(a);//a1是a的副本
    string a2 = a2;//等价上面
    string a4(5,'c');//由5个c组成
    system("pause");
    return 0;
}

Common operations for string (brushing questions):

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
    
       
    string a = "a123";
    a.empty();//看a是不是空
    a.size();//看a的长度
    system("pause");
    return 0;
}

Look at a string show operation

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
    
       
    string a;
    cin>>a;//我输入的是"    我喜欢你   "
    cout<<a;//输出的是”我喜欢你“
    system("pause");
    return 0;
}

The type returned by ps:a.size() is the size_type type (unsigned integer). Errors may occur when using int to accept it. It is recommended to use auto b = a.size();
string operations:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
    
       
    string a = "今天天气真好,";
    string b = "我可以爱你吗?";
    cout<<a+b;//字符串直接组合,前面的必须是字符串,"a" + b这样就不行。
    cout<<endl;
    if(a > b) cout<<"a大";//进行比较看第一个字符的ascll码,哪个大就是哪个大,第一个一样比较第二个,以此类推。
    else cout<<"b大";
    system("pause");
    return 0;
}

Standard library for judging whether there are alphanumeric characters in a string: Usage of cctype
for each:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
    
       
    string a = "我需要你";
    for(auto ai : a){
    
    
        cout<<ai;
    }
    system("pause");
    return 0;
}

3.3, standard library vector

This is a class template, which is used to store data.
I really don't like to enumerate these functions one by one. I don't pay much attention to it. Leetcode often uses this data structure. link

3.4, iterator

Used to move from the current character to the next character without using subscripts.
Both string and vector support iterators (I feel that this is faster than subscripts)

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
    
       
    string a = "someone";
    if(a.begin() != a.end()){
    
    //a.begin()指向第一个元素s,a.bagin()指向最后一个元素e后面的位置
        auto it = a.begin();
        cout<< *it++;//先进行的输出,再进行的迭代器++,也就是指向下一个位置的操作
        cout<< *it;//迭代器是一个指针
    }
    auto *it1 = a.cbegin();
    auto *it2 = a.cend();
    //加不加c里面内容一样,都是第一个和最后一个下一个位置,只不过类型就算常量迭代器。
    system("pause");
    return 0;
}

ps: The iterator of vector will fail due to operations such as push_back().
Iterator addition and subtraction are all iterators move several positions. To add is to move backward, and to subtract is to move forward.

3.5, array

Emm initialization or something , others wrote well (lazy, linking
the pointer to the array actually points to the first storage space of the array, for example, string nums[]={"1","2"} pointer points to the address stored in 1.
c Conversion of style and string:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
    
       
    string a = "someone";
    const char *str=a.c_str();
    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45743162/article/details/115117772