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;
}
相关推荐
科学的发展-只不过是读大自然写的代码5 分钟前
Qt 日志输出的选择方案有多少
开发语言·qt
小白学大数据11 分钟前
爬虫进阶:Selenium与Ajax的无缝集成
大数据·开发语言·爬虫·selenium·ajax
取加若则_18 分钟前
C++入门(C语言过渡)
c语言·开发语言·数据结构·c++·算法
极度的坦诚就是无坚不摧19 分钟前
Python 数据容器的对比
开发语言·python
真果粒wrdms24 分钟前
【在线词典】项目实现
linux·c语言·嵌入式硬件·算法·udp·sqlite3
onetwo_23325 分钟前
python实现接口自动化
开发语言·python·自动化
YangZheng@28 分钟前
23种设计模式
c++·算法·设计模式
互联网架构小马31 分钟前
12种增强Python代码的函数式编程技术
开发语言·后端·python·函数式编程
会编程的果子君1 小时前
人工智能系列-Python面向对象编程
开发语言·python
当年拼却醉颜红1 小时前
力扣爆刷第161天之TOP100五连刷71-75(搜索二叉树、二维矩阵、路径总和)
算法·leetcode·矩阵