std常用函数

std::move

将一个左值引用强制转化为右值引用

//调用移动构造函数,掏空str,掏空后,最好不要使用str
std::string str = "Hello";
v.push_back(std::move(str));//str会变成空,左值变成了右值,str消失了

 std::remove_reference

去掉引用,int&   变成 int

int test_remove_reference()
{
  int a[] = {1,2,3};
  //decltype(*a) b = a[0]; //decltype(*a)= int&
  remove_reference<decltype(*a)>::type b = a[0]; //int&变成了 int
  a[0] = 4;
  cout << b;
  return 0;
}

std::function

模板类

 通过std::function对C++中各种可调用实体(普通函数、Lambda表达式、函数指针、以及其它函数对象等)的封装,形成一个新的可调用的std::function对象;让我们不再纠结那么多的可调用实体。一切变的简单粗暴。

使用时好比函数指针

#include <iostream>
#include <map>
#include <functional>
using namespace std;
 
// 普通函数
int add(int i, int j) { return i + j; }
// lambda表达式
auto mod = [](int i, int j){return i % j; };
// 函数对象类
struct divide
{
int operator() (int denominator, int divisor)
{
return denominator / divisor;
}
};
 
///SubMain//
int main(int argc, char *argv[])
{
// 受限的map
map<char, int(*)(int, int)> binops_limit;
binops_limit.insert({ '+', add });
binops_limit.insert({ '%', mod });
// 错误 1 error C2664: “void std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert(std::initializer_list<std::pair<const _Kty,_Ty>>)”: 无法将参数 1 从“initializer-list”转换为“std::pair<const _Kty,_Ty> &&”
// binops_limit.insert({ '%', divide() });
 
// 更灵活的map
map<char, function<int(int, int)>> binops = 
{
{ '+', add },
{ '-', minus<int>() },
{ '*', [](int i, int j){return i - j; } },
{ '/', divide() },
{ '%', mod },
};
cout << binops['+'](10, 5) << endl;
cout << binops['-'](10, 5) << endl;
cout << binops['*'](10, 5) << endl;
cout << binops['/'](10, 5) << endl;
cout << binops['%'](10, 5) << endl;
system("pause");
return 0;
}
///End Sub//

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::vector是C++标准库中的一个容器,提供了丰富的成员函数和操作符,用于方便地对元素进行访问、插入、删除和修改等操作。以下是std::vector常用的一些函数: 1. push_back:将元素添加到vector的末尾。 ```cpp std::vector<int> vec; vec.push_back(10); // 添加元素10到vector的末尾 ``` 2. pop_back:删除vector的最后一个元素。 ```cpp std::vector<int> vec = {10, 20, 30}; vec.pop_back(); // 删除最后一个元素,vec变为{10, 20} ``` 3. size:返回vector中元素的个数。 ```cpp std::vector<int> vec = {10, 20, 30}; int size = vec.size(); // size的值为3 ``` 4. empty:检查vector是否为空。 ```cpp std::vector<int> vec; bool isEmpty = vec.empty(); // isEmpty的值为true ``` 5. clear:清空vector中的所有元素。 ```cpp std::vector<int> vec = {10, 20, 30}; vec.clear(); // 清空vector,vec变为空 ``` 6. at:访问指定位置的元素,带有边界检查。 ```cpp std::vector<int> vec = {10, 20, 30}; int element = vec.at(1); // element的值为20 ``` 7. front:返回vector的第一个元素。 ```cpp std::vector<int> vec = {10, 20, 30}; int firstElement = vec.front(); // firstElement的值为10 ``` 8. back:返回vector的最后一个元素。 ```cpp std::vector<int> vec = {10, 20, 30}; int lastElement = vec.back(); // lastElement的值为30 ``` 9. erase:删除指定位置或指定范围内的元素。 ```cpp std::vector<int> vec = {10, 20, 30, 40, 50}; vec.erase(vec.begin() + 2); // 删除第3个元素,vec变为{10, 20, 40, 50} vec.erase(vec.begin() + 1, vec.begin() + 3); // 删除第2个和第3个元素,vec变为{10, 50} ``` 10. insert:在指定位置插入元素。 ```cpp std::vector<int> vec = {10, 20, 30}; vec.insert(vec.begin() + 1, 15); // 在第2个位置插入元素15,vec变为{10, 15, 20, 30} ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值