C ++コンテナの遺伝的アルゴリズムの要約

C ++遺伝的アルゴリズム

1。概要

遺伝的アルゴリズムは、アルゴリズム、数値、およびその他のヘッダーファイルにあります。

読み取り専用アルゴリズム:

蓄積(beg、end、start)
count(beg、end、n)
vector<int> ve{1,2,3,4,4,5,6,43,2,1,23,4};
    auto beg=ve.cbegin();
    auto end=ve.cend();
    int sum=accumulate(beg,end,0);
    cout<<sum<<endl;
    cout<<count(beg,end,4)<<endl;

コンテナアルゴリズムの記述:

アルゴリズムは書き込み操作をチェックしません

fill(beg、end、s):[beg、end);の範囲でsを書き込みます。
fill_n(beg、n、s)
vector<int> ve{1,2,3,4,4,5,6,43,2,1,23,4};
    auto beg=ve.begin();
    auto end=ve.end();
    fill(beg,end,2);
    for(int i:ve){
        cout<<to_string(i)+" ";
    }
    cout<<endl;
vector<int> ve{1,2,3,4,4,5,6,43,2,1,23,4};
    auto beg=ve.begin();
    auto end=ve.end();
    fill_n(beg,5,2);
    for(int i:ve){
        cout<<to_string(i)+" ";
    }
    cout<<endl;

上記のnが大きすぎると、メモリオーバーフローが発生します

back_insert:イテレータを挿入し、挿入位置を指すコンテナにイテレータをバインドします

次の方法では、メモリがオーバーフローしません。

vector<int> ve{1,2,3,4,4,5,6,43,2,1,23,4};
    auto beg=ve.begin();
    auto end=ve.end();
    auto it=back_inserter(ve);
    int i=0;
    fill_n(it,10,4);
    for(int i:ve){
        cout<<to_string(i)+" ";
    }
    cout<<endl;

コピーアルゴリズム

copy(begin(a1)、end(a1)、a2)

配列をコピーする

int a1[]={1,2,3,4,5};
    int a2[5];
    copy(begin(a1),end(a1),a2);
    for (int i = 0; i < 5; ++i) {
        cout<<a2[i]<<endl;
    }

replace(beg、end、oldValue、newValue):

イテレータ範囲の古い値を新しい値に置き換えます。正規表現はサポートされていません。

 vector<string> ve{"aa","bb","cc","dd","ee","dd","cc","bb","aa"};
    auto beg=ve.begin();
    auto end=ve.end();
    replace(beg,end,"aa","xx");
    for(auto s:ve){
        cout<<s<<endl;
    }
replace_copy(beg、end、back_inserter(veCopy)、“ aa”、“ xx”):

元のコンテナの値を変更せずに、変更した値を別のコンテナにコピーします。

vector<string> ve{"aa","bb","cc","dd","ee","dd","cc","bb","aa"};
vector<string> veCopy;
auto beg=ve.begin();
auto end=ve.end();
replace_copy(beg,end,back_inserter(veCopy),"aa","xx");
for(auto s:veCopy){
    cout<<s<<" ";
}
cout<<endl;
for(auto s:ve){
    cout<<s<<" ";
}
cout<<endl;

2.コンテナのリロードの算術

unique(words.begin()、words.end()):

イテレータ範囲内の要素を再配置し、各要素を1回だけ保存して、最後の一意の要素の後の位置を返します。

#include<algorithm>
#include<numeric>
using namespace std;
void elimDups(vector<string> &);
int main() {
    vector<string> ve={"the","quick","fox","jumps","over","the","slow","red","turtle"};
    elimDups(ve);
    for(string s:ve){
        cout<<s<<endl;
    }
}
void elimDups(vector<string> &words){

    sort(words.begin(),words.end());
    auto end_unique=unique(words.begin(),words.end());
    words.erase(end_unique,words.end());
}

3.カスタマイズされた操作

sort(iterator、iterator、pred expression)、

並べ替えの基礎として関数を渡す

#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
#include<numeric>
using namespace std;
void elimDups(vector<string> &);
int main() {
    vector<string> ve={"the","quick","fox","jumps","over","the","slow","red","turtle"};
    sort(ve.begin(),ve.end(),[](const string &s1,const string& s2){return s1.size()<s2.size();});
    for(string s:ve){
        cout<<s<<endl;
    }
}

find_if(ve.begin()、ve.end()、[sz](const string&s){return s.size()> sz;})

以下のfind_ifは、ラムダ式に一致する最初の値を検索し、キャプチャされた値を使用します。

#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
#include<numeric>
using namespace std;
void elimDups(vector<string> &);
int main() {
    vector<string> ve={"the","quick","fox","jumps","over","the","slow","red","turtle"};
    int sz=4;
    auto w=find_if(ve.begin(),ve.end(),[sz](const string &s){return s.size()>sz;});
    cout<<*w<<endl;
    /*for(string s:ve){
        cout<<s<<endl;
    }*/
}
for_each(ve.begin()、ve.end()、[](const string&s){cout << s << "";})

for_eachは、イテレーター範囲のパラメーターに対してラムダ式のパラメーターを実行します。

#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
#include<numeric>
using namespace std;
void elimDups(vector<string> &);
int main() {
    vector<string> ve={"the","quick","fox","jumps","over","the","slow","red","turtle"};
    int sz=4;
    for_each(ve.begin(),ve.end(),[](const string &s){cout<<s<<" ";});
}
最大(vector <string>&、vector :: size_type)

コンテナを最小から最大の長さに配置し、同じ長さの単語を辞書式順序にします。

void biggest(vector<string> &words,vector<string>::size_type sz){
    elimDups(words);

    stable_sort(words.begin(),words.end(),[](const string &a,const string &b){return a.size()<b.size();});

    auto wc=find_if(words.begin(),words.end(),[sz](const string &a){return a.size()>=sz;});

    auto count=words.end()-wc;

    cout<<count<<" "<<"words"<<endl;

    for_each(wc,words.end(),[](const string &s){cout<<s<<" ";});
    cout<<endl;
}
void elimDups(vector<string> &words){
    sort(words.begin(),words.end());
    auto end_unique=unique(words.begin(),words.end());
    words.erase(end_unique,words.end());
}

暗黙のキャプチャ

count_if(ve.begin()、ve.end()、[=](const string&s){return s.size()> 4;})

イテレータの範囲で長さが4より大きい文字列を検索します。=は暗黙的なキャプチャの表現であり、コピーを意味し、参照を意味します。

#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
#include<numeric>

using namespace std;
void elimDups(vector<string> &);
void biggest(vector<string> &,vector<string>::size_type);
int main() {
    vector<string>::size_type sz=4;
    vector<string> ve={"the","quick","fox","jumps","over","the","slow","red","turtle"};
    auto n=count_if(ve.begin(),ve.end(),[=](const string &s){return s.size()>4;});
    cout<<n<<endl;

}

自動check_sz = bind(check_size、_1、sz);

_1名前空間プレースホルダーでは、関数check_sizeの最初のパラメーターがszにバインドされていることを意味します

bind()関数を使用して、関数とパラメーターをバインドします

#include <functional>
using namespace std;
void elimDups(vector<string> &);
bool check_size(const string &a,string::size_type sz);
void biggest(vector<string> &,vector<string>::size_type);
using namespace placeholders;
int main() {
    vector<string>::size_type sz=4;
    vector<string> ve={"the","quick","fox","jumps","over","the","slow","red","turtle"};
    auto check_sz=bind(check_size,_1,sz);
    auto n=count_if(ve.begin(),ve.end(),check_sz);
    cout<<n<<endl;

}
bool check_size(const string &a,string::size_type sz){
    return a.size()>sz;
}
auto isSmaller=bind(compared,_2,_1);
cout<<compared(3,1)<<endl;
cout<<isSmaller(3,1)<<endl;

4.ストリームイテレータ

istream_iterator <int>は入力ストリームからデータを取得し、Int_eofはストリームの終わりをマークします
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <regex>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {


    istream_iterator<int> int_it(std::cin),int_eof;
    vector<int> ve(int_it,int_eof);
    for(int i:ve){
        cout<<i<<"  ";
    }
}


ostream_iterator

//ostream_iterator<T> out(os) 构造方法,out将类型为T的值写入输入流OS
//ostream_iterator<T> out(os,c) 构造方法,out将类型为T的值写入输入流OS,并在每个值后加一个c
istream_iterator<int> int_it(std::cin),int_eof;
vector<int> ve(int_it,int_eof);
ostream_iterator<int> out_iter(cout," ");
for (auto e:ve) {
    //使用迭代器给cout赋值,并将迭代器向后移一位;
    *out_iter++=e;
}
ファイル入力ストリームのイテレータを取得します
#include <iostream>
#include <fstream>
#include <zconf.h>
#include <sstream>
#include <iterator>
#include "resource/PersonInfo.h"
using namespace std;

int main() {
    vector<PersonInfo> ve;
    PersonInfo personInfo;
    ifstream in("D:/CLProj/IO/resource/a.text");
    //得到流的迭代器和尾后迭代器,一旦IO遇到错误或读到文件结尾,迭代器的值就会和尾后迭代器相等;
    istream_iterator<string> in_iter(in), in_end;

    while (in_iter!=in_end){
        cout<<*in_iter++<<endl;
    }
}
ファイル出力ストリームのイテレータを取得します
#include <iostream>
#include <fstream>
#include <zconf.h>
#include <sstream>
#include <iterator>
#include "resource/PersonInfo.h"
using namespace std;

int main() {
    vector<PersonInfo> ve;
    PersonInfo personInfo;
    personInfo.setName("何宇");
    personInfo.setPhones({"1231221234","12131357624"});
    ve.push_back(personInfo);
    ofstream out("D:/CLProj/IO/resource/a.text",ofstream::app);
    ostream_iterator<string> in_iter(out," ");
    for(PersonInfo pI:ve){
        *in_iter++=pI.name;
        for(auto phone:pI.phones)
        *in_iter++=phone;
        *in_iter++="\n";
    }
}
何必 13788889999 13277778888
吴洋 18955557777 13877772222
鲁中 17833332222 17833332222
郭德纲 13922228888
李云 17233339999 18712348976
汪悦 17233332299 18712444476
何世清 18023458921    13476324587
何宇 1231221234 12131357624

逆イテレータ

ここに画像の説明を挿入します

#include <iostream>
#include <fstream>
#include <zconf.h>
#include <sstream>
#include <iterator>
#include "resource/PersonInfo.h"
using namespace std;

int main() {
//    反向迭代器的结尾指向迭代器开头之前的一个位置
    string s("Hello World!");
    auto rbeg=s.crbegin();
    auto rend=s.crend();
    while (rbeg!=rend){
        cout<<*rbeg<<"  ";
        ++rbeg;
    }
}

総括する:

unique(beg,end) //排重算法
uniuqe(beg,end,comp)//使用comp来比较两个元素是否相等,即调用==来判断是否相等
find(beg,end,val)//在迭代器范围内寻找变量val第一次出现的位置
find_if(beg,end,pred)//查找第一个令pred判断为真的元素
reverse(beg,end)//反转迭代器内的元素
reverse(beg,end,dest)//将反转后的元素拷贝至dest,不改变原来的值;
remove_if(beg,end,lambda)//lambda表达式为真,则将迭代器范围内的值移除
remove_if(v1.beg,v1.end,back_inserter(v2),pred)//将移除的元素拷贝到v2中
replace(beg,end,oldValue,newValue)//在迭代器范围内使用新值代替旧值
replace_if(beg,end,pred,newValue)//,如果元素使pred为真在迭代器范围内使用新值代替旧值。
replace_copy_if(beg,end,dest,pred,newValue)//如果元素使pred为真在迭代器范围内使用新值代替旧值,并将旧值拷贝至dest中
list和forward_list容器中的算法:
list.merge(list2) 将list2中的元素并入list,并且将List2中的元素删除,两者必须是有序的,合并后使用<排序
list.merge(list2,comp) 将list2中的元素并入list,并且将List2中的元素删除,使用comp中的规则排序
list.remove(val)  删除元素val;
list.remove(val,pred),删除使函数为真的元素
list.reverse(),是list的元素反转;
list.sort() 按照默认顺序以"<"排序
list.sort(comp) 以comp比较大小排序
list.unique() 删除重复元素
list.unique(comp) 使用给定的二元谓词判断是否相等	

おすすめ

転載: blog.csdn.net/m0_47202518/article/details/109178771