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;
}
相关推荐
武子康7 分钟前
大数据-212 数据挖掘 机器学习理论 - 无监督学习算法 KMeans 基本原理 簇内误差平方和
大数据·人工智能·学习·算法·机器学习·数据挖掘
IT技术分享社区25 分钟前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
极客代码28 分钟前
【Python TensorFlow】入门到精通
开发语言·人工智能·python·深度学习·tensorflow
疯一样的码农35 分钟前
Python 正则表达式(RegEx)
开发语言·python·正则表达式
passer__jw76735 分钟前
【LeetCode】【算法】283. 移动零
数据结构·算法·leetcode
Ocean☾42 分钟前
前端基础-html-注册界面
前端·算法·html
顶呱呱程序1 小时前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
&岁月不待人&1 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
StayInLove1 小时前
G1垃圾回收器日志详解
java·开发语言
无尽的大道1 小时前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化