1、hash函数的应用:根据入参hash出一个唯一的hash id
原型:hash函数对象类:用法像函数的对象,看注释
std::hash<std::string> str_hash; // 定义一个对象
size_t res3 = str_hash(str1); // 调用该对象的括号运算符()函数
代码:
#include <functional>
#include <string>
int main()
{
char nts1[] = "Test";
char nts2[] = "Test";
std::string str1(nts1);
std::string str2(nts2);
std::hash<char *> ptr_hash; // 根据chiar指针进行hash
std::hash<std::string> str_hash; // 根据string内容进hash
size_t res1 = ptr_hash(nts1);
size_t res2 = ptr_hash(nts2); // 1和2不一样
size_t res3 = str_hash(str1);
size_t res4 = str_hash(str2); // 3和4一样
getchar();
return 0;
}
原理: hash id由入参唯一确定,返回值类型为size_t:size_t is an unsigned integral type.
参考链接:std::hash
2、find_if库函数:
函数原型:
template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
返回值:找到第一个返回true(返回值可转换为bool)的元素的迭代器指针
代码:
#include <algorithm> // std::find_if
#include <vector> // std::vector
bool IsOdd(int i)
{
return ((i % 2) == 1);
}
int main()
{
std::vector<int> myvector;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(40);
myvector.push_back(55);
std::vector<int>::iterator it = std::find_if(myvector.begin(), myvector.end(), IsOdd);
std::cout << "The first odd value is " << *it << '\n';
return 0;
}
函数解释:对首尾迭代器中元素执行第三个参数指定的函数,结果为true则直接返回当前迭代器,为false则继续对下一个元素执行,全为false则返回尾迭代器。
参考链接:std::find_if
std::count_if:与find_if类似
参考链接:std::count_if
3、string.find:查找第一个匹配的字符串,返回首字母的下标
代码:
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
std::size_t found = str.find(str2); // 返回str2与str匹配时第一个字符在str中的下标
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
参考链接:string.find
4、std::atomic应用:防止多线程数据竞争
说明: Objects of atomic types contain a value of a particular type (T).
The main characteristic of atomic objects is that access to this contained value from different threads cannot cause data races (i.e., doing that is well-defined behavior, with accesses properly sequenced). Generally, for all other objects, the possibility of causing a data race for accessing the same object concurrently qualifies the operation as undefined behavior.
代码:关注注释
// constructing atomics
#include <iostream> // std::cout
#include <atomic> // std::atomic, std::atomic_flag, ATOMIC_FLAG_INIT
#include <thread> // std::thread, std::this_thread::yield
#include <vector> // std::vector
std::atomic<bool> ready (false); // 原子对象,bool类型,初始化为false
std::atomic_flag winner = ATOMIC_FLAG_INIT;
void count1m (int id) {
while (!ready) { std::this_thread::yield(); } // wait for the ready signal
for (volatile int i=0; i<1000000; ++i) {} // go!, count to 1 million
if (!winner.test_and_set()) { std::cout << "thread #" << id << " won!\n"; }
};
int main ()
{
std::vector<std::thread> threads; // 多线程都需要访问该原子对象,不会竞争
std::cout << "spawning 10 threads that count to 1 million...\n";
for (int i=1; i<=10; ++i) threads.push_back(std::thread(count1m,i));
ready = true;
for (auto& th : threads) th.join();
return 0;
}
参考链接:std::atomic