C++实践积累

C++ STL

vector

如何彻底清空一个vector?
实践证明,vector.clear()并不能把vector容量清空,只会让vector.size()变为零,依然很占内存。那如何让vector的capacity变为0呢?

vector<int> nums(500);
nums.clear();
cout<<"size after clear:"<<nums.size()<<endl;
cout<<"capacity after clear:"<<nums.capacity()<<endl;
nums = vector<int>();
cout<<"capacity after from new vector:"<<nums.capacity()<<endl;

输出结果:
C++实践积累

string

substr

s.substr(start, nums);    //从下标为start的元素开始,取nums个数。注意:nums表示所取字符个数,而不结尾字符的下标。
s.substr(start);    //从下标为start的元素开始,取到s的结尾

列如,

string str="We think in generalities, but we live in details.";
string str2 = str.substr (3,5);     // "think"
string str3 = str.substr (3);     //think in generalities, but we live in details.
size_t pos = str.find("live");      // position of "live" in str
string str3 = str.substr (pos);     // get from "live" to the end

set

set 用来为C++中的集合容器,但是是底层是用红黑树实现的,其迭代器不能加减常数。但是如何还想用set迭代器加减常数,可以使用vector模拟一个set。每次修改vector之后,需要使用del_dup函数除去重复元素。

void del_dup(vector<int> &vec) {
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

虽说set迭代器不能加减常数,但提供了.begin(),.end(),it++等操作,所以遍历集合set可以有以下两种方式:
方法一:auto for

    for (auto i : myset) {
        cout<<i<<" ";
    }

方法二:迭代器

    for (auto it = myset.begin(); it != myset.end(); it++) {
        cout<<*it<<" ";
    }

C++11新用法

哈希表

C++11中引用哈希表来加快查找内容的速度,但是名字不叫hash_map,而叫unordered_set与unordered_map,使用时,

#include <unordered_set>
#include <unordered_map>

unordered_set

unordered_set可以在常数时间查找表中的内容,当我们需要确定曾经是否访问过某个内容,使用unordered_set可以实现O(1)时间查找。比如图的遍历中,确定是否曾经访问过某个结点,可以一边遍历,一边构建hash表,一边查边,确保没有重复访问结点,具体实现参考2.4 图的层序遍历

unodered_map

unodered_map当我们需要建立某键值对{key, val}之间的关系,并且随后需要根据key快速查找value时,可以使用unordered_map。
创建方法

unordered_map<int, string> hash;

初始化

unordered_map<int, string> hash = {{1, "milk"},{2, "flour"}};

插入

hash.insert({3, "apple"});

查找

auto got = hash.find(1);
if (got != hash.end()) {    //使用前最好先确定确实找到了键值对
    cout<<got->first<<endl;
    cout<<got->second<<endl;
}

输出

1
milk

当我们需要深度拷贝图,或者带随机指针的链表时,可以使用该方法。

遍历容器不同方法

C++引入了一种更简单的for语句, 这种语句可以更简洁遍历初始值列表、数组、容器、string等类型的对象,这些对象的公共特点是都有返回迭代器的begin与end成员。
主要形式,如下:

for (auto &element : vector) {
    statement;
}

其中

  • auto用来确定容器中元素的类型,不仅可以用在for中,也可以用在其它位置用来确定定义的变量类型,如:auto it = vector.begin() 这样写可以比较简洁
  • 如果需要改写容器中元素内容比较在element前加引用型&

以下代码为遍历一个容器或者数组或者string的所有方式:

    int array[] = {2, 4, 6, 8};
    vector<int> vec(array, array + 4);
    string str = "I am good student";

    cout<<endl<<"method1: iterator"<<endl;
    for (vector<int>::iterator it = vec.begin(); it < vec.end(); ++it) {
        cout<<*it<<" ";
    }

    cout<<endl<<"method2: index"<<endl;
    for (vector<int>::size_type id = 0; id < vec.size(); ++id) {
        cout<<vec[id]<<" ";
    }

    cout<<endl<<"method3: auto type"<<endl;
    //sometimes, you need iterator traverse while simple code
    for (auto it = vec.begin(); it < vec.end(); ++it) {
        cout<<*it<<" ";
    }

    cout<<endl<<"method4: auto for"<<endl;
    //use & for writing element to vector
    for (auto item : vec) {
        cout<<item<<" ";
    }

    cout<<endl<<"method5: auto for to traverse array"<<endl;
    for (auto item : array) {
        cout<<item<<" ";
    }
    cout<<endl;
    for (auto item : {1, 3, 5, 7}) {
        cout<<item<<" ";
    }

    cout<<endl<<"method6: auto for to traverse string"<<endl;
    for (auto c : str) {
        cout<<c;
    }

C++与python语言切换时的一些定式思维

二维数组的访问

python中二维数组中可以使用[row, col]的方式访问二维数组
C++实践积累
但是C++中逗号表达式表示顺序执行所有表达式,并且将最后一个表达式的值返回。C++ A[i, j]其实表示的是A[j],只是一个指针而已,这会带来很大的误解。

原文链接: https://www.cnblogs.com/fariver/p/7136219.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++实践积累

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/256503

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午10:08
下一篇 2023年2月14日 上午10:08

相关推荐