我的C++日记本

每天最高兴的事情就是可以学到新的东西。
(所有资料均来自网络查询)

2020-4-14:

1.lower_bound(begin,end,num)
从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
2.upper_bound(begin,end,num)
从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
常用格式:
lower_bound(begin,end,num)-begin;
得到的是num的底标
upper_bound(begin,end,num)-begin;
得到的是第一个比num大的底标
注:lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的,尽量别用于有重复元素的数组。
3.remove(还未完全明白。。。)

2020-4-15

1.remove(begin(),end(),val)
原码:
返回一个最末尾的迭代器。

template <class ForwardIterator, class T>
  ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator result = first;
  while (first!=last) {
    if (!(*first == val)) {
      *result = move(*first);
      ++result;
    }
    ++first;
  }
  return result;//返回一个最末尾的迭代器。
}

测试:
1:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s="123456";
	auto r=remove(s.begin(),s.end(),'2');
	cout<<s.size()<<endl;
	for(auto it=s.begin();it<r;++it){
		cout<<*it<<" ";
	}
	cout<<endl;
	for(auto it=s.begin();it<s.end();++it){
		cout<<*it<<" ";
	}
}

/*
6
1 3 4 5 6
1 3 4 5 6 6
*/

2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s="12324256";
	auto r=remove(s.begin(),s.end(),'2');
	cout<<s.size()<<endl;
	for(auto it=s.begin();it<r;++it){
		cout<<*it<<" ";
	}
	cout<<endl;
	for(auto it=s.begin();it<s.end();++it){
		cout<<*it<<" ";
	}
}
/*
8
1 3 4 5 6
1 3 4 5 6 2 5 6
*/

看都remove的原码默默的在浏览器打上了move四个字母。。。。

2.move(val):
std::move是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝所以可以提高利用效率,改善性能.。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string t="321";
	string n="123";
	cout<<&t<<endl;
	cout<<&n<<endl;
	t=move(n);
	cout<<&n<<endl;
	cout<<&t<<endl;
	cout<<"t:"<<t<<endl;
	cout<<"n:"<<n<<endl;
}//值交换但是地址并没有改变。
/*
0x73fe00
0x73fdf0
0x73fdf0
0x73fe00
t:123
n:321
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t=1;
	int n=2;
	cout<<&t<<endl;
	cout<<&n<<endl;
	t=move(n);
	cout<<&n<<endl;
	cout<<&t<<endl;
	cout<<"t:"<<t<<endl;
	cout<<"n:"<<n<<endl;
}//改为int后,t=n,但是n并没有改变。
/*
0x73fe0c
0x73fe08
0x73fe08
0x73fe0c
t:2
n:2
*/

3.set < int >()可表示空集合,可用于清空集合。
set< int > s;
s=set< int > ();

4.set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s,s.begin()))取集合交集
s得到的是s1和s2的交集。

5.set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s,s.begin()))取集合并集
s得到的是s1和s2的并集。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值