首页
统计
关于
友联
Search
1
断链是什么意思?
607 阅读
2
判断点的凹凸性
555 阅读
3
C++中的const
442 阅读
4
Python基础
437 阅读
5
C++ map遍历的几种方式
429 阅读
默认分类
Cpp
Python
登录
/
注册
Search
标签搜索
Python
Dare to Dream
累计撰写
24
篇文章
首页
栏目
默认分类
Cpp
Python
页面
统计
关于
友联
搜索到
11
篇与
的结果
2022-10-30
此内容被密码保护
加密文章,请前往内页查看详情
2022年10月30日
150 阅读
0 点赞
2022-10-29
此内容被密码保护
加密文章,请前往内页查看详情
2022年10月29日
138 阅读
1 点赞
2022-10-27
C++find_if算法
C++find_if算法功能描述:按条件查找元素函数原型:find_if(iterator beg, iterator end, _Pred); //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置 //beg开始迭代器 //end结束迭代器 //_Pred函数或者谓词(返回bool类型的仿函数)代码示例:#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; //常用查找算法find_if //1.查找内置数据类型 class GreaterFive { public: bool operator()(int val) { return val > 5; } }; void test01() { vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive()); if (it == v.end()) { cout << "没有找到" << endl; } else { cout << "找到大于5的数字为:" << *it << endl; } } //2.查找自定义数据类型 class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; class Greater20 { public: bool operator()(Person &p) { return p.m_Age > 20; } }; void test02() { vector<Person>v; //创建数据 Person p1("aaa", 10); Person p2("bbb", 20); Person p3("ccc", 30); Person p4("ddd", 40); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); //找年龄大于20的人 vector<Person>::iterator it= find_if(v.begin(), v.end(), Greater20()); if (it == v.end()) { cout << "没有找到" << endl; } else { cout << "找到姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl; } } int main() { test01(); test02(); system("pause"); return 0; }
2022年10月27日
140 阅读
1 点赞
2022-10-27
C++ map遍历的几种方式
C++ map遍历的几种方式#include <iostream> #include <unordered_map> using namespace std; int main() { unordered_map<string, int> mp; mp["张三"] = 20; mp["李四"] = 18; mp["王五"] = 30; // 方式一、迭代器 cout << "方式一、迭代器" << endl; for (auto it = mp.begin(); it != mp.end(); it++) { cout << it -> first << " " << it -> second << endl; } // 方式二、range for C++ 11版本及以上 cout << "\n方法二、 range for" << endl; for (auto it : mp) { cout << it.first << " " << it.second << endl; } // 方法三、 C++ 17版本及以上 cout << "\n方法三" << endl; for (auto [key, val] : mp) { cout << key << " " << val << endl; } return 0; }运行结果方式一、迭代器 王五 30 李四 18 张三 20 方法二、 range for 王五 30 李四 18 张三 20 方法三 王五 30 李四 18 张三 20 补充C++ 提供 map 与 unordered_map 两种关联容器,可以将 key 与 value 关联起来。map 与 unordered_map 区别:底层实现原理map: map 内部实现了一个红黑树,该结构具有自动排序的功能,因此map内部的所有元素都是有序的,红黑树的每一个节点都代表着map的一个元素,因此,对于map进行的查找,删除,添加等一系列的操作都相当于是对红黑树进行这样的操作,故红黑树的效率决定了map的效率。unordered_map: unordered_map内部实现了一个哈希表,因此其元素的排列顺序是杂乱的,无序的。查询效率 unordered_map 查询复杂度O(1), map查询复杂度O(logn)运行效率方面:unordered_map最高,而map效率较低但 提供了稳定效率和有序的序列。占用内存方面:map内存占用略低,unordered_map内存占用略高,而且是线性成比例的。
2022年10月27日
429 阅读
1 点赞
2022-08-24
C++中的const
1、指向const的指针const修饰“最靠近”它的那个。这样,如果要使正指向的元素不发生改变,得写一个像这样的定义。 const int* u;从标识符开始,是这样读的:“u是一个指针,它指向一个const int。”这里不需要初始化,因为u可以指向任何标识符(也就是说,指针u不是一个cosnt),但它所指的值是不能被改变的。 int const* v; 和第一种形式的效果一样,但为了程序更具有可读性,应该坚持用第一种形式。$\sqrt{2}$[Latex]\sqrt{2}[/Latex]
2022年08月24日
442 阅读
1 点赞
1
2
3