C++ 的常见算法 之一

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;
}
相关推荐
2401_8574396918 分钟前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_1 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Dream_Snowar2 小时前
速通Python 第三节
开发语言·python
唐诺2 小时前
几种广泛使用的 C++ 编译器
c++·编译器
XH华2 小时前
初识C语言之二维数组(下)
c语言·算法
南宫生3 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
不想当程序猿_3 小时前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
高山我梦口香糖3 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
落魄君子3 小时前
GA-BP分类-遗传算法(Genetic Algorithm)和反向传播算法(Backpropagation)
算法·分类·数据挖掘