学习vc++的第三天--集合

原来cpp拷贝构造函数写了之后一定要重写赋值运算符,否则会出现内存重复释放的问题.


学习了集合类型,发现cpp的vector的_Pop_back_n()函数已经在vs2019无法使用..貌似作用也不大..嘻嘻

cpp的vector和c#的list差不多,而cpp的list却好像没有对应的c#操作...

因为我在c#上面重写过迭代器支持类型,
也就是一个不重复加入的:list<T1,T2,T3>,
c#自带的支持重复和仅一个泛型的模板:list<T>,
所以可能会在cpp上面复现过往实现过的功能,就比较敏感集合类型的用途.

vector的意思是矢量,矢量是连续的,
而list是链表,链表是可断可接,
如意所示,速度要求就看名字就可以联想得到,给他们起名字的人都挺有意思的.

然后发现cpp的std(标准模板库)的没有驼峰命名法,蛮糟糕的,因为敲开c#的人都喜欢以此区分函数和变量...

cpp挺有意思,int不能写成int,要写成size_t...因为考虑了64位版本...
所以很多时候一定要多看内部代码..

今天学习到字符串的赋值和操作,主要操作的形式是要对char.. char*.. const char*.. 然后和string..str.c_str()的认识...

乱七八糟的学习代码:

#include <iostream>
#include <vector>
#include <list>
#include <map>
using namespace std;

int main()
{
    {
        vector<int> arr_int;
        vector<int> arr_int2(4, 88);
        vector<int> arr_int3 = arr_int2;
        vector<int> arr_int4(arr_int3.begin(), arr_int3.end());
        for (size_t i = 0; i < 6; i++)
        {
            arr_int4.push_back(666);
        }

        int szint[10] = { 2,3,6,9,8,7,899,4 };
        //拷贝的首地址,拷贝的长度
        vector<int> arr_int5(szint, szint + sizeof(szint) / sizeof(int));
        int a = arr_int5[5];
        cout << "a=" << a << endl;
        cout << "int5 的大小是:" << arr_int5.size() << endl;
        cout << "11111 =" << arr_int5.front() << endl;
        cout << "end =" << arr_int5.back() << endl;
        //arr_int5.clear();

        {
            vector<string*> arr_string;
            string* str;
            string stra = "试试";
            str = &stra;
            arr_string.push_back(str);
            arr_string.push_back(str);
            arr_string.push_back(str);
            stra = "hhhhhh";
            arr_string.push_back(str);
            arr_string.push_back(str);
        }


        if (arr_int5.size() > 5)
        {
            arr_int5.insert(arr_int5.begin() + 2, 3, 666);
            int szint2[] = { 15, 25,35 };
            arr_int5.insert(arr_int5.end(), szint2, szint2 + sizeof(szint2) / sizeof(int));
            arr_int5.pop_back();
            arr_int5.erase(arr_int5.begin() + 2, arr_int5.begin() + 4);//从什么范围删到什么范围 
        }

        //遍历操作
        for (int idx = 0; idx < arr_int5.size(); ++idx)
        {
            int value = arr_int5[idx];
            cout << value << ",";
        }
        cout << endl;
        //用迭代器遍历,迭代器也就是用指针的开头和结束 
        for (vector<int>::iterator itor = arr_int5.begin(); itor != arr_int5.end(); ++itor)
        {
            int value = *itor;//解引用
            cout << value << ",";
        }
        cout << endl;

        list<int> ls(arr_int5.begin(), arr_int5.end());

        list<int> ls2 = { 1,4,3,3,3,7,9,3,6,8,3,5,2,3,7 };
        //ls2.remove(3);//这样就可以删掉3了耶,非题目要求的for方法

        //用for方法
        for (list<int>::iterator itor = ls2.begin(); itor != ls2.end();)
        {
            int value = *itor;//解引用
            if (*itor == 3)
            {
                //移除之后迭代器销毁了,然后要传回去下一个迭代器
                itor = ls2.erase(itor);
            }
            else
            {
                itor++;
            }
        }

        for (list<int>::iterator itor = ls2.begin(); itor != ls2.end(); itor++)
        {
            int value = *itor;//解引用
            cout << value << ",";
        }
        cout << endl;

        map<int, char> stud_sex_map;
        int n_size = stud_sex_map.size();
        bool is_empty = stud_sex_map.empty();//是否为空
        stud_sex_map[10010] = 'm';
        stud_sex_map[10011] = 'f';
        char ch = stud_sex_map[10010];

        if (!stud_sex_map.count(10012) > 0)//!含有
        {
            stud_sex_map[10012] = 'm';
        }
        ch = stud_sex_map[10012];
        stud_sex_map.erase(10010);
        stud_sex_map.erase(--stud_sex_map.end());

        //遍历键值对
        for (map<int, char> ::iterator itor = stud_sex_map.begin(); itor != stud_sex_map.end(); ++itor)
        {
            int key = itor->first;
            char ch = itor->second;
            cout << "key =" << key << ",value = " << ch << endl;
        }
        cout << endl;
        cout << endl;
        cout << endl;


        map<int, char> rilige;
        rilige[10010] = 'm';
        rilige[10013] = 'm';
        rilige[10015] = 'm';

        rilige[10011] = 'f';
        rilige[10012] = 'f';
        rilige[10014] = 'f';
        //是自动排序的
        for (map<int, char> ::iterator itor = rilige.begin(); itor != rilige.end(); )
        {
            int key = itor->first;
            char ch = itor->second;
            if (ch == 'f')
            {
                //移除之后迭代器销毁了,然后要传回去下一个迭代器
                itor = rilige.erase(itor);
            }
            else
            {
                itor++;
                cout << "key =" << key << ",value = " << ch << endl;
            }
        }


        {
            string str;
            str.assign("aaaaaaaaaaaaaa");
            cout << str << endl;
            const char* p23 = "hello, cctry.com";

            str.clear();
            str.assign(p23);//指针赋值法
            cout << str << endl;

            str.clear();
            str.assign(p23, 7, 91);//指针赋值法
            cout << str << endl;

            cout << str + str << endl;

            str = "hello cctry.com, byebye. cctry.com";
            //str = "hello cctry.com, byebye. ";
            size_t ipos = str.find("cctry.com");//返回找到的位置
            if (ipos > 0)//就是找到了
            {
                const char* ca = str.c_str();
                size_t ipos2 = str.find("cctry.com", ipos + 1);//返回找到的位置,要加1才能够找下一个
                if (ipos2 > 0)//就是找到了
                {

                }
            }
            str.replace(0, 5, "zhangsan");//替换 
            cout << str << endl;

            str.insert(0, "lisi ");
            cout << str << endl;

            str.insert(0, 3, 'v');
            cout << str << endl;
        }

        {
            vector<char> vec;
            string vecstring;//存放新的
            string strasd("123#ab##cctry.com#");
            for (size_t i = 0; i < strasd.size(); i++)
            {
                char pn = strasd[i];
                if (pn != '#')
                {
                    vec.push_back(pn);
                    vecstring.push_back(pn);
                }
            }
        }
          

    }
    std::cin.get();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/JJBox/p/12535967.html