c++相关要点

// #include <iosteam>
using namespace std;

void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i)
cout << v.capacity() << endl;
}
 
}
//vector 的构造
void test02(){
vector <int>v;
int arr[] = {1,2,3,4,};
vector<int> v1(arr,arr + sizeof(arr)/sizeof(int))
vector<int> v2(v1.bengin(),v1.end());
//赋值
vector<int> v3(10,100)
vecor<int>v4;
v4.assigin(v3.begin(),v3.end())
//交换
v2.swap(v2);
//重新分配空间
v2.resize(10);//分配10个空间
v2.resize(10,-1);//分配的10个空间用-1来填充
v2.capacity();//容量
v2.size();//大小
/*插入和删除
insert(const_iterator pos, int count,ele)迭代器指向位置pos插入count个元素ele
push_back(ele) 尾部插入元素ele
pop_back() 删除最后一个元素
erase(const_iterator start, const_iterator end) 删除迭代器从start到end之间的元素
erase(const_iterator pos) 删除迭代器指向的元素
clear() 删除容器中所有的元素
*/
//逆序遍历
for(vector<int>::reverse_iterator it = v.rbegin(); it != v.rend(); it++)
{
cout << *it << endl;
}

/**
* deque双端插入和删除操作
* push_back(elem) 在容器尾部添加数据
* push_front(elem) 在容器头部添加数据
* pop_back() pop_front() 删除容器最后/第一个数据
* deque数据存取
* at(idx) 返回索引idx 所指的数据,如果idx越界,抛出out_of_range
* operator[] 返回索引idx所指的数据,如果idx越界,不抛出异常,直接报错
* front() back() 返回第一/最后一个数据
* deque插入操作
* insert(pos,elem) 在pos位置插入一个elem元素,返回新数据位置
* insert(pos,n,elem) 在pos位置插入n个elem数据,无返回值
* insert(pos,beg,end) 在pos位置插入[beg,end)区间的数据,无返回值
* clear() erase(beg,end) erase(pos)
*/
 
/***
* queue
* queue<int>q
* q.push(10)
* q.pop
* q.empty()
* q.front()
* q.back()
* ***/

}

bool myCompare(int v1,int v2){
return v1 > v2
}
test03(){

/***
* list双向链表
*/
list<int> myList;
for(int i = 0; i < 10; i++){
myList.push_back(i);
}
list<int>::_Nodeptr node = myList._Myhead->_Next;
for (int i = 0; i < myList._mysize * 2; i++)
{
node = node->_Next;
if (node == myList._Myhead)
{
node = node->_Next;
}
 
}
list<int>L;
//所有不支持随机访问的迭代器 不可以用系统提供的算法
//如果不支持系统提供的算法,这个类内部会提供相应的方法
L.reverse();
L.sort();
L.sort(myCompare);

 
}

class Person{
public:
Person(string name, Int age){
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
//重载 让remove可以删除自定义类型
bool operator == (const Person & p){
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
return false;
 
}
}

bool myComparePerson(Person &p1, Person &p2){
if (p1.m_age > p2.m_age) 
{
return true;
}
return false;
 
}
test04(){
list<Person> L;
Person p1("" 10);
L.push_back(p1)
L.sort(myComparePerson)
//remove需要自定义类重装==方法
L.remove(p1)
}

test05(){
/**
* find(key)
* count(key)
* lower_bound(keyElem)//返回第一个key>=keyElem元素的迭代器
* upper_bound(keyElem) 返回第一个key>keyElem元素的迭代器
* equal_range(keyElem) 返回容器中key与keyElem相等的上下限的两个迭代器
*/
set<int> s1;
s1.inset(5);
s1.find(5)
pair<set<int>::iterator,set<int>::iterator> ret = s1.equal_range(3)
if (ret.first ! = s1.end())
{
cout << "找到" << *(ret.first) << endl;
}else{
cout << "为找到" << endl;
}
 
if (re.second != s1.end)
{
cout<<"找到" <<*(re.second)<<endl;
}
 
}

void test06(){
/***
* pair创建方式
* ***/
//1
pair<string,int> p(string("tom"),100)
cout<<p.first<<p.second<<endl;
//2.
pair<string,int> p2 = make_pair("",200)
}

//set容器 指定set排序规则(使用仿函数)
class myCompareSet{
public:
//重载 ()
bool operator()(int v1, int v2){
return v1 > v2;
}
}
class myCompareSetPerson{
public:
//重载 ()
bool operator()(Person &v1, Person &v2){
return v1.m_name > v2.m_name
}
}

//指定排序规则
class myComparemap{
public:
bool operator()(int v1, int v2){
return v1 > v2;
}
}
void test07(){
set<int, myCompareSet> s1;
s1.insert(1);
for (set<int,myCompareSet>::iterator it = s1.begin(); it!=s1.end(),it++)
{
cout<<*it<<endl;
}

set<Person,myCompareSetPerson> s1;
Person p1 = Person("name",100);
s1.insert(p1)
for (set<Person,myCompareSet>::iterator it = s1.begin();it != s1.end(),it++)
{
cout<<"name"<<(*it).name<<endl;
}

map<int,int> m;
m.insert(pair<int,int>(1,10))
m.insert(make_pair(2,20))
m.insert(map<int,int>::value_type(3,30))
m[4] = 40;
for (set<int,int>::iterator it = m.begin();it != m.end(),it++)
{
cout<<"name"<< it->first<< it->second <<endl;
}

pair<map<int,int>::iterator,map<int,int>::iterator> ret = m.equal_range(3)
if (ret.first != m.end())
{
cout<<"找到"<<ret.first->first<<ret.first->second<<endl;
}
if (ret.second != m.end())
{
cout<<"找到"<<ret.second->first<<ret.second->second<<endl;
}
map<int,int>::iterator pos = m.find(0);
int num = m.count(3); //0 或者1 

//使用排序规则
map<int, int,myComparemap> mm;
mm.insert(pair<int,int>(1,10))
mm.insert(make_pair(2,20))
mm.insert(map<int,int>::value_type(3,30))
mm[4] = 40;
//multimap的key是可以重复的
}



class MyPrint{
public:
void operator()(int num){
count++;
}
int coun = 0
}
void MyPrint2(int num)
{
cout<<num<<endl;
}

//一元
class GreaterThen20{
public:
void operator()(int val){
return val > 20
}
}
//二元谓词
class mytwoComparetwo{
bool operator()(int v1,int v2){return v1 > v2;}
}
//谓词
void test08(){
vector<int> v;
vector<int>::iterator pos = find_if(v.begin(),v.end(),GreaterThen20)
cout<<*pos<<endl; 
 
sort(v.begin(),v.end(),[](int val){cont<<val<<endl;})
}
//内建函数
void test09(){
//#include<functional>
negate<int>n;
cont<<n(10)<<endl;
plus<int> p;
cont<<p(1,1)<<endl;
vector<int> v;
sort(v.begin(),v.end(),greater<int>())
sort(v.begin(),v.end(),less <int>())
}
 
 
/***
* template<class T> T puls<T>
* template<class T> T minus<T>
* template<class T> T multiplies <T>
* template<class T> T divides<T>
* template<class T> T modulus<T> 取模
* template<class T> T megate<T>
*
* template<class T> bool equal_to<T>
* template<class T> bool not_equal_to<T>
* template<class T> bool greater<T>
* template<class T> bool greater_equal<T>
* template<class T> bool less<T>
* template<class T> bool less_equal<T>
*
* template<class T> bool logical_and<T>
* template<class T> bool logical_or<T>
* template<class T> bool logical_not<T>
*
* **/
 
 
 
 
 
 
 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

//1.2.参数类型,3.返回值
class MyPrint:public binary_function<int,int,void> 
{

 
public:
// void operator()(int v){
// cout << v << endl;
// }
void operator()(int v, int start) const
{
cout << v + start << endl;
}
MyPrint(/* args */);
~ MyPrint();
};

MyPrint:: MyPrint(/* args */)
{
}

MyPrint::~ MyPrint()
{
}

void test01(){
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// for_each(v.begin(),v.end(),MyPrint());
cout << "请输入起始值"<<endl;
int num;
cin >> num;
// for_each(v.begin(),v.end(),bind2nd(MyPrint(),num));
for_each(v.begin(),v.end(),bind1st(MyPrint(),num));
}



class GreaterThenFive: public unary_function<int,bool>
{
private:
/* data */
public:
bool operator()(int v) const
{
return v > 5;
}
GreaterThenFive(/* args */);
~GreaterThenFive();
};

GreaterThenFive::GreaterThenFive(/* args */)
{
}

GreaterThenFive::~GreaterThenFive()
{
}

//取反
void test02(){
//一元
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator pos = find_if(v.begin(),v.end(),not1(GreaterThenFive()));
vector<int>::iterator pos1 = find_if(v.begin(),v.end(),not1(bind2nd(greater<int>(),5)));
if (pos != v.end())
{
cout <<"找到的数字"<< *pos<<endl;
cout <<"找到的数字"<< *pos1<<endl;
 
}
 
 
}


//函数指针
void MyPrint3(int v,int start){
cout << v + start <<endl;
}
void test03(){
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//将函数指针适配为函数对象 ptr_fun
for_each(v.begin(),v.end(),bind2nd(ptr_fun(MyPrint3),100));
}


class PersonBase
{
private:
/* data */
public:
string name;
int age;
PersonBase(string name, int age);
void showPerson(){
cout << "naem: " << name << " " << "age: " << age;
}
void plusAge(){
this->age = this->age + 10;
}
~PersonBase();
};

PersonBase::PersonBase(string name, int age)
this->age = age;
this->name = name;
}

PersonBase::~PersonBase()
{
}
void MyPrintPerson(PersonBase &p){
cout << "name:" << p.name << " " << "age:" << p.age << endl;
}


void test04(){
vector<PersonBase>v;
PersonBase p1("aaa",10);
PersonBase p2("bbb",11);
v.push_back(p1);
v.push_back(p2);
//成员函数适配器
for_each(v.begin(),v.end(),MyPrintPerson);
for_each(v.begin(),v.end(),mem_fun_ref(&PersonBase::showPerson));
for_each(v.begin(),v.end(),mem_fun_ref(&PersonBase::plusAge));
for_each(v.begin(),v.end(),mem_fun_ref(&PersonBase::showPerson));
}
 
void test05(){
vector<*PersonBase>v;
PersonBase p1("aaa",10);
PersonBase p2("bbb",11);
v.push_back(&p1);
v.push_back(&p2);
//成员函数适配器
for_each(v.begin(),v.end(),MyPrintPerson);
for_each(v.begin(),v.end(),mem_fun(&PersonBase::showPerson));
for_each(v.begin(),v.end(),mem_fun(&PersonBase::plusAge));
for_each(v.begin(),v.end(),mem_fun(&PersonBase::showPerson));
}

int main(int argc, const char** argv) {
// test01();
// test02();
// test03();
test04();
return 0;
}












 

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <algorithm>

#include <vector>

#include <functional>

#include <string>

 

using namespace std;

 

void myPrint(int v){

    cout << v << endl;

};

class myPrint001

{

public:

    void operator()(int v){

        cout << v << endl;

    }

};

struct myPrint01

{

    void operator()(int v){

        cout << v << endl;

    }

};

void test01(){

    vector<int> v;

    for (int i = 0; i < 10; i++) {

        v.push_back(i);

    }

//    myPrint(),myPrint001()

    for_each(v.begin(), v.end(), myPrint01());

}

 

 

struct myPrint02

{

    int m_Count;

    void operator()(int v){

        cout << v << endl;

        cout << m_Count++;

    }

};

void test02(){

    vector<int> v;

    for (int i = 0; i < 10; i++) {

        v.push_back(i);

    }

    myPrint02 print2 = for_each(v.begin(), v.end(), myPrint02());

    cout << print2.m_Count << endl;

}

//绑定参数输出

struct myPrint03:public binary_function<intintvoid>

{

    

    void operator()(int v,int start) const

    {

        cout << v  + start << endl;

        

    }

};

void test03(){

    vector<int> v;

    for (int i = 0; i < 10; i++) {

        v.push_back(i);

    }

    for_each(v.begin(), v.end(), bind2nd(myPrint01(), 1000));

}

 

/**

 transform算法 将指定容器区间元素搬到另一个容器,不分配内存

 */

class TransForm

{

public:

    int operator()(int val)

    {

        return val + 10;

    }

};

class TransForm2

{

public:

    int operator()(int val, int val2)

    {

        return val + val2;

    }

};

void test04(){

    vector<int> v;

    vector<int> vTarget;

    for (int i = 0; i < 10; i++) {

        v.push_back(i);

    }

    vTarget.resize(v.size());

    for_each(v.begin(), v.end(), bind2nd(myPrint01(), 1000));

    transform(v.begin(), v.end(), vTarget.begin(), TransForm());

}

 

void test05(){

    vector<int> v1;

    vector<int> v2;

    vector<int> vTarget;

    for (int i = 0; i < 10; i++) {

        v1.push_back(i);

        v2.push_back(i + 100);

    }

    vTarget.resize(v1.size());

    transform(v1.begin(), v1.end(), v2.begin(), vTarget.begin(), TransForm2());

}

 

 

/**

 find查找算法

 */

void test06(){

    vector<int> v;

    for (int i = 0; i < 10; i++) {

        v.push_back(i);

    }

    vector<int>::iterator pos = find(v.begin(), v.end(), 5);

    if (pos != v.end()) {

        cout << "找到数据" << *pos <<endl;

    }

}

class Person {

    

public:

    string name;

    int age;

    Person(string name, int age){

        this->name = name;

        this->age = age;

    }

    bool operator==(const Person &p){

        if (this->age == p.age && this->name == p.name) {

            return true;

        }else{

            return false;

        }

    }

};

class MyComparePerson{

public:

    bool operator()(Person *p1)

    {

        if (p1->name == "bbb" && p1->age == 20) {

            return true;

        }

    }

};

class MyComparePerson01:public binary_function<Person *,Person *,bool>

{

public:

    bool operator()(Person *p1, Person *p2) const

    {

        if (p1->name == p2->name && p1->age == p2->age) {

            return true;

        }

    }

};

void test0601(){

    vector<Person>v;

    vector<Person *>v1;

    Person p1("aaa", 10);

    Person p2("bbb", 11);

    Person p3("ccc", 12);

    v.push_back(p1);

    v.push_back(p2);

    

    v1.push_back(&p1);

    v1.push_back(&p2);

    vector<Person>::iterator pos = find(v.begin(), v.end(), p1);

    if (pos != v.end()) {

        cout << "找到" << (*pos).name << "年龄:" << pos->age << endl;

    }

    vector<Person *>::iterator pos1 = find_if(v1.begin(), v1.end(), MyComparePerson());

    vector<Person *>::iterator pos2 = find_if(v1.begin(), v1.end(), bind2nd(MyComparePerson01(), p1));

}

 

/**

 adjacent_find 查找相邻重复元素 第一个重复序列位置

 binary_search二分查找   序列必须是有序的

 count统计元素出现次数

 */

class greaterThenFour

{

public:

    bool operator()(int v)

    {

        return v > 4;

    }

};

void test07(){

    vector<int> v;

    v.push_back(1);

    v.push_back(2);

    v.push_back(3);

    v.push_back(4);

    v.push_back(6);

    v.push_back(7);

    vector<int>::iterator pos = adjacent_find(v.begin(), v.end());

    bool ret = binary_search(v.begin(), v.end(), 4);

    int num = count(v.begin(), v.end(), 4);

    num = count_if(v.begin(), v.end(), greaterThenFour());

}

 

 

/**

 *merge 容器合并,并存储到另一个容器,这俩个容器必须是有序的

 */

void test08(){

    vector<int> v1;

    vector<int> v2;

    for (int i = 0; i < 10; i++) {

        v1.push_back(i);

        v2.push_back(i + 10);

    }

    vector<int> vTarget;

    vTarget.resize(v1.size() + v2.size());

    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

    for_each(vTarget.begin(), vTarget.end(), [](int v){ cout<< v <<endl;});

    //cout << v << " ";

}

 

 

 

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>
#include<iterator>
#include<numeric>
using namespace std;

struct myPrint01
{
void operator()(int v){
cout << v << endl;
}
};

class MyCompare{
public:
bool operator()(int v){
return v > 3;
}
}
void test01(){
vector<int> v;
vector<int> v1;
v.push_back(10);
v.push_back(11);
v.push_back(12);
v.push_back(13);
v.push_back(14);
v.push_back(15);
v1.push_back(31);
v1.push_back(32);
v1.push_back(33);
v1.push_back(34);
// sort(v.begin(),v.end());
// for_each(v.begin(), v.end(), myPrint01());
// random_shuffle(v.begin(),v.end());
// reverse(v.begin(),v.end());
vector<int> vTarget;
vTarget.resize(v.size());
// copy(v.begin(),v.end(),vTarget.begin());
for_each(v.begin(), v.end(), myPrint01());
 
//需求, 把容器中的3替换
replace(v.begin(),v.end(), 3, 300);
//大于3的数替换为4
replace_if(v.begin(),v.end(),MyCompare(),4);
copy(vTarget.begin(),vTarget.end(),ostream_iterator<int>(cout," "));
//交换数据
swap(v1,v);
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 
}

//算法
/**
* accumulate 计算和
* fill 向容器中填充元素
*/
void test02()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
int sum = accumulate(v.begin(),v.end(),0);
cout << sum;

vector<int>v1;
v1.resize(10);
fill(v1.begin(),v1.end(),10);
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
}

/**
* 常用集合算法 两个set的集合必须是有序序列
* set_intersection 两个集合的交集
* set_difference 求两个set集合的差集
* set_union 算法求两个set集合的并集
*
*/
void test03()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int> vTarget;
vTarget.resize(min(v1.size(),v2.size()));
vector<int>::iterator itEnd = set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),vTarget.begin());
copy(vTarget.begin(),itEnd,ostream_iterator<int>(cout, " "));
vector<int> vTarget_union;
vTarget_union.resize(v1.size() + v2.size());
vector<int>::iterator itend_union = set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),vTarget_union.begin());
vector<int>vTarget_diff;
vTarget_diff.resize(max(v1.size(),v2.size()));
vector<int>::iterator itend_dif = set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),vTarget_diff.begin());
}

 

 

posted @ 2020-12-31 02:06  sundaysc++  阅读(49)  评论(0编辑  收藏  举报