C++ 的常见算法 之一
不修改序列算法
for_each
#include <iostream> // std::cout
#include <algorithm> // std::for_each
#include <vector> // std::vector
using namespace std;
struct packet {
int id;
int sz;
};
void myfunction(int i) { // function:
cout << ' ' << i;
}
void print_packet(packet pkt) {
cout << pkt.id << " Size:" << pkt.sz << endl;
}
struct myclass { // function object type:
void operator() (int i) { std::cout << ' ' << i; }
} myobject;
int for_each_algo() {
vector<int> myvector;
vector<packet> ptks = {
{0x700, 50},
{0x701, 90},
{0x702, 1000} };
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
cout << "myvector contains:";
for_each(myvector.begin(), myvector.end(), myfunction);
cout << '\n';
// or:
std::cout << "myvector contains:";
for_each(myvector.begin(), myvector.end(), myobject);
std::cout << '\n';
cout << "myvector contains:";
for_each(ptks.begin(), ptks.end(), print_packet);
cout << '\n';
return 0;
}
count
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct addr {
string street_addr;
string city;
string postcode;
bool operator==(const addr& addr1) {
if (postcode != addr1.postcode)
return 0;
return 1;
}
};
int main () {
// counting elements in array:
int myints[] = { 10,20,30,30,20,10,10,20 }; // 8 elements
int mycount = std::count(myints, myints + 8, 10);
cout << "10 appears " << mycount << " times.\n";
// counting elements in container:
vector<int> myvector(myints, myints + 8);
mycount = std::count(myvector.begin(), myvector.end(), 20);
cout << "20 appears " << mycount << " times.\n";
vector<addr> addrs = {
{"1024 merivale Rd", "ottawa", "K1Z 6A5"},
{"1025 merivale Rd", "ottawa", "K1Z 6A5"},
{"335 blossom rd", "san jose", "95123"},
{"5970 lean st", "san jose", "95123"},
{"5821 falon way", "san jose", "95123"},
};
addr myaddr = { "", "", "95123" };
mycount = count(addrs.begin(), addrs.end(), myaddr);
cout << "95123 appears " << mycount << " times.\n";
addr myaddr2 = { "", "", "K1Z 6A5" };
mycount = count(addrs.begin(), addrs.end(), myaddr2);
cout << "95123 appears " << mycount << " times.\n";
return 0;
}
find
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct addr {
string street_addr;
string city;
string postcode;
int operator==(const addr& addr1) {
if (street_addr != addr1.street_addr)
return 0;
if (city != addr1.city)
return 0;
if (postcode != addr1.postcode)
return 0;
return 1;
}
};
int main() {
// using std::find with array and pointer:
int myints[] = { 10, 20, 30, 40 };
int* p;
p = find(myints, myints + 4, 30);
if (p != myints + 4)
cout << "Element found in myints: " << *p << '\n';
else
cout << "Element not found in myints\n";
// using std::find with vector and iterator:
vector<int> myvector(myints, myints + 4);
vector<int>::iterator it;
it = find(myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
cout << "Element found in myvector: " << *it << '\n';
else
cout << "Element not found in myvector\n";
vector<addr> addrs = {
{"1024 merivale Rd", "ottawa", "K1Z 6A5"},
{"1025 merivale Rd", "ottawa", "K1Z 6A5"},
};
vector<addr>::iterator it_addr;
addr myaddr = { "1024 merivale Rd", "ottawa", "K1Z 6A6" };
it_addr = find(addrs.begin(), addrs.end(), myaddr);
if (it_addr != addrs.end()) {
cout << "Address: " << it_addr->street_addr << '\n';
cout << "City: " << it_addr->city << '\n';
cout << "Post Code: " << it_addr->postcode << '\n';
}
else
cout << "Address not found in addrs\n";
return 0;
}
修改序列算法
copy
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print_val(int v) {
cout << " " << v;
};
int copy_algo() {
int myints[] = { 10,20,30,40,50,60,70 };
vector<int> myvector(7);
copy(myints, myints + 7, myvector.begin());
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
cout << "for_each: myvector contains:";
for_each(myvector.begin(), myvector.end(), print_val);
cout << endl;
return 0;
}
move
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> foo = { "air","water","fire","earth" };
vector<string> bar(4);
// moving ranges:
cout << "Moving ranges...\n";
move(foo.begin(), foo.begin() + 4, bar.begin());
cout << "foo contains " << foo.size() << " elements:";
cout << " (each in an unspecified but valid state)";
cout << '\n';
cout << "bar contains " << bar.size() << " elements:";
for (auto x : bar) cout << " [" << x << "]";
cout << '\n';
// moving container:
cout << "Moving container...\n";
foo = move(bar);
cout << "foo contains " << foo.size() << " elements:";
for (auto x : foo) cout << " [" << x << "]";
cout << '\n';
cout << "bar is in an unspecified but valid state";
cout << '\n';
return 0;
}