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;
}
相关推荐
敲上瘾3 分钟前
操作系统的理解
linux·运维·服务器·c++·大模型·操作系统·aigc
福大大架构师每日一题5 分钟前
文心一言 VS 讯飞星火 VS chatgpt (396)-- 算法导论25.2 1题
算法·文心一言
不会写代码的ys9 分钟前
【类与对象】--对象之舞,类之华章,共绘C++之美
c++
兵哥工控12 分钟前
MFC工控项目实例三十二模拟量校正值添加修改删除
c++·mfc
在下不上天12 分钟前
Flume日志采集系统的部署,实现flume负载均衡,flume故障恢复
大数据·开发语言·python
EterNity_TiMe_20 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
长弓聊编程22 分钟前
Linux系统使用valgrind分析C++程序内存资源使用情况
linux·c++
陌小呆^O^26 分钟前
Cmakelist.txt之win-c-udp-client
c语言·开发语言·udp
cherub.29 分钟前
深入解析信号量:定义与环形队列生产消费模型剖析
linux·c++
机器学习之心31 分钟前
一区北方苍鹰算法优化+创新改进Transformer!NGO-Transformer-LSTM多变量回归预测
算法·lstm·transformer·北方苍鹰算法优化·多变量回归预测·ngo-transformer