例如声明如下的模板:
template
void foo(T val);
如果调用使用:
int x;
foo(std::ref(x));
T变成int&,而使用调用
int x;
foo(std::cref(x));
T变成const int&。
这个特性被C++标准库用在各个地方,例如:
make_pair()用此特性于是能够创建一个pair<>of reference
make_tuple()用此特性可以创建一个tuple<>of reference
std::vector coll; //error
std::vector> coll; //ok
#include
#include
#include
using namespace std;
void func(int x, int y) {
std::cout << "func" << std::endl;
}
class C {
public:
void memfunc(int x, int y) const{
std::cout << "C::memfunc" << std::endl;
}
};
int main()
{
std::vector> tasks;
tasks.push_back(func);
tasks.push_back([](int x, int y) {
std::cout << "lambda" << std::endl;
});
for (std::function f : tasks) {
f(3, 33);
}
std::function mf;
mf = &C::memfunc;
mf(C(), 2, 3);
return 0;
}
输入:
func
lambda
C::memfunc
auto extremes = std::minmax({px, py, pz}, [](int*a, int*b) {
return *a < *b;
});
namespace std {
template
inline void swap(T& a, T& b) {
T tmp(std::move(a));
a = std::move(b);
b = std::move(tmp);
}
}
#include
#include
using namespace std;
int main()
{
typedef ratio<5,3> FiveThirds;
cout << FiveThirds::num << "/" << FiveThirds::den << endl;
typedef ratio<25,15> AlsoFiveThirds;
cout << AlsoFiveThirds::num << "/" << AlsoFiveThirds::den << endl;
ratio<42,42> one;
cout << one.num << "/" << one.den << endl;
ratio<0> zero;
cout << zero.num << "/" << zero.den << endl;
typedef ratio<7,-3> Neg;
cout << Neg::num << "/" << Neg::den << endl;
}
输出:
5/3
5/3
1/1
0/1
-7/3
#include
#include
#include
using namespace std;
int main()
{
std::chrono::duration twentySeconds(20); //以秒为单位
std::chrono::duration> halfAMinute(0.5); //以60秒为单位
std::chrono::duration> oneMillisecond(1); //以1/1000秒为单位
std::chrono::seconds twentySeconds(20);
std::chrono::hours aDay(24);
std::chrono::milliseconds oneMillisecond(1);
}
// 将毫秒单位的duration切割为小时,分钟,秒,毫秒
#include
#include
#include
#include
using namespace std;
using namespace std::chrono;
milliseconds ms(7255042);
template
ostream& operator<<(ostream& os, const chrono::duration& d) {
os << "[" << d.count() << " of " << R::num << "/" << R::den << "]";
return os;
}
// 将毫秒单位的duration切割为小时,分钟,秒,毫秒
int main()
{
hours hh = duration_cast(ms);
minutes mm = duration_cast(ms%chrono::hours(1));
seconds ss = duration_cast(ms%chrono::minutes(1));
milliseconds msec = duration_cast(ms%chrono::seconds(1));
cout << "raw: " << hh << "::" << mm << "::"
<< ss << "::" << msec << endl;
cout << " " << setfill('0') << setw(2) << hh.count() << "::"
<< setw(2) << mm.count() << "::"
<< setw(2) << ss.count() << "::"
<< setw(2) << msec.count() << endl;
}
#include
#include
#include
#include
using namespace std;
using namespace std::chrono;
template
void printClockData ()
{
using namespace std;
cout << "- precision: ";
// if time unit is less than or equal to one millisecond
typedef typename C::period P; // type of time unit
if (ratio_less_equal::value) {
// convert to and print as milliseconds
typedef typename ratio_multiply
::type TT;
cout << fixed << double(TT::num)/TT::den
<< " milliseconds" << endl;
}
else {
// print as seconds
cout << fixed << double(P::num)/P::den << " seconds" << endl;
}
cout << "- is_steady: " << boolalpha << C::is_steady << endl;
}
int main()
{
std::cout << "system_clock: " << std::endl;
printClockData();
std::cout << "\nhigh_resolution_clock: " << std::endl;
printClockData();
std::cout << "\nsteady_clock: " << std::endl;
printClockData();
}
输出:
system_clock:
- precision: 0.000001 milliseconds
- is_steady: false
high_resolution_clock:
- precision: 0.000001 milliseconds
- is_steady: false
steady_clock:
- precision: 0.000001 milliseconds
- is_steady: true
下面的程序将timepoint赋值给tp,并转换为日历表示法,window运行会报错,LInux下运行正常:
#include
#include
#include
#include
std::string asString (const std::chrono::system_clock::time_point& tp)
{
// convert to system time:
std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::string ts = std::ctime(&t); // convert to calendar time
ts.resize(ts.size()-1); // skip trailing newline
return ts;
}
int main()
{
// print the epoch of this system clock:
std::chrono::system_clock::time_point tp;
std::cout << "epoch: " << asString(tp) << std::endl;
// print current time:
tp = std::chrono::system_clock::now();
std::cout << "now: " << asString(tp) << std::endl;
// print minimum time of this system clock:
tp = std::chrono::system_clock::time_point::min();
std::cout << "min: " << asString(tp) << std::endl;
// print maximum time of this system clock:
tp = std::chrono::system_clock::time_point::max();
std::cout << "max: " << asString(tp) << std::endl;
}
输出:
epoch: Thu Jan 1 08:00:00 1970
now: Thu Nov 30 21:29:29 2023
min: Tue Sep 21 08:18:27 1677
max: Sat Apr 12 07:47:16 2262
#include
#include
#include
#include
using namespace std;
string asString (const chrono::system_clock::time_point& tp)
{
time_t t = chrono::system_clock::to_time_t(tp); // convert to system time
string ts = ctime(&t); // convert to calendar time
ts.resize(ts.size()-1); // skip trailing newline
return ts;
}
int main()
{
// define type for durations that represent day(s):
typedef chrono::duration> Days;
// process the epoch of this system clock
chrono::time_point tp;
cout << "epoch: " << asString(tp) << endl;
// add one day, 23 hours, and 55 minutes
tp += Days(1) + chrono::hours(23) + chrono::minutes(55);
cout << "later: " << asString(tp) << endl;
// process difference from epoch in minutes and days:
auto diff = tp - chrono::system_clock::time_point();
cout << "diff: "
<< chrono::duration_cast(diff).count()
<< " minute(s)" << endl;
Days days = chrono::duration_cast(diff);
cout << "diff: " << days.count() << " day(s)" << endl;
// subtract one year (hoping it is valid and not a leap year)
tp -= chrono::hours(24*365);
cout << "-1 year: " << asString(tp) << endl;
// subtract 50 years (hoping it is valid and ignoring leap years)
tp -= chrono::duration>(50);
cout << "-50 years: " << asString(tp) << endl;
// subtract 50 years (hoping it is valid and ignoring leap years)
tp -= chrono::duration>(50);
cout << "-50 years: " << asString(tp) << endl;
}
输出:
epoch: Thu Jan 1 08:00:00 1970
later: Sat Jan 3 07:55:00 1970
diff: 2875 minute(s)
diff: 1 day(s)
-1 year: Fri Jan 3 07:55:00 1969
-50 years: Thu Jan 16 07:55:00 1919
-50 years: Wed Jan 27 08:00:43 1869
#include
#include
#include
#include
// convert timepoint of system clock to calendar time string
inline std::string asString(const std::chrono::system_clock::time_point& tp) {
std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::string ts = ctime(&t); // convert to calendar time
ts.resize(ts.size()-1); //skip trailing newline
return ts;
}
// convert calender time to timepoint of system clock
inline std::chrono::system_clock::time_point
makeTimePoint(int year, int mon, int day, int hour, int min, int sec=0) {
struct std::tm t;
t.tm_sec = sec;
t.tm_min = min;
t.tm_hour = hour;
t.tm_mday = day;
t.tm_mon = mon - 1;
t.tm_year = year-1900;
t.tm_isdst = -1;
std::time_t tt = std::mktime(&t);
if (tt == -1) {
throw "no valid system time";
}
return std::chrono::system_clock::from_time_t(tt);
}
int main() {
auto tp1 = makeTimePoint(2023, 11, 30, 00, 00);
std::cout << asString(tp1) << std::endl;
auto tp2 = makeTimePoint(2023, 03, 23, 12, 33);
std::cout << asString(tp2) << std::endl;
return 0;
}
输出:
Thu Nov 30 00:00:00 2023
Thu Mar 23 12:33:00 2023
//在ptr所指的前len个byte中找出字符c
memchr(const void* ptr, int c, size_t len)
//比较ptr1和ptr2所指的前len个byte
memcmp(const void* ptr1, const void* ptr2, size_t len)
//将fromPtr所指的前len个byte复制到toPtr
memcpy(void* toPtr, const void* fromPtr, size_t len)
//将fromPtr所指的前len个byte复制到toPtr(区域可重叠)
memmove(void* toPtr, const void* fromPtr, size_t len)
//将ptr所指的前len个byte赋值为字符c
memset(void* ptr, int c, size_t len)
#include
#include
#include
using namespace std;
int main()
{
list coll;
// insert elements from 20 to 40
for (int i=20; i<=40; ++i) {
coll.push_back(i);
}
// find position of element with value 3
// - there is none, so pos3 gets coll.end()
auto pos3 = find (coll.begin(), coll.end(), // range
3); // value
// reverse the order of elements between found element and the end
// - because pos3 is coll.end() it reverses an empty range
reverse (pos3, coll.end());
// find positions of values 25 and 35
list::iterator pos25, pos35;
pos25 = find (coll.begin(), coll.end(), // range
25); // value
pos35 = find (coll.begin(), coll.end(), // range
35); // value
// print the maximum of the corresponding range
// - note: including pos25 but excluding pos35
cout << "max: " << *max_element (pos25, pos35) << endl;
// process the elements including the last position
cout << "max: " << *max_element (pos25, ++pos35) << endl;
}
查找最先出现的25或者35
#include
#include
#include
using namespace std;
int main()
{
list coll;
// insert elements from 20 to 40
for (int i=20; i<=40; ++i) {
coll.push_back(i);
}
auto pos = find_if(coll.begin(), coll.end(),
[](int i) {
return i == 25 || i == 35;
});
if (pos == coll.end()) {
std::cout << "not found" << std::endl;
exit(1);
}
list::const_iterator pos25, pos35;
if (*pos == 25) {
// 先找到25
pos25 = pos;
pos35 = find(++pos, coll.end(), 35);
std::cout << *pos35 << std::endl;
} else {
pos35 = pos;
pos25 = find(++pos, coll.end(), 25);
std::cout << *pos25 << std::endl;
}
}
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
list coll1 = {1, 2, 3, 4, 5, 6, 7, 8};
vector coll2;
copy(coll1.begin(), coll1.end(), back_inserter(coll2));
deque coll3;
copy(coll1.begin(), coll1.end(), front_inserter(coll3));
set coll4;
copy(coll1.begin(), coll1.end(), inserter(coll4, coll4.begin()));
for (auto &ele : coll2) {
std::cout << ele << " ";
}
std::cout << std::endl;
for (auto &ele : coll3) {
std::cout << ele << " ";
}
std::cout << std::endl;
for(auto & ele : coll4) {
std::cout << ele << " ";
}
}
输出:
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
inserter的作用是在“初始化时接受的第二个实参”所指的位置的前面插入元素,内部调用成员函数insert(),并以新值和新位置作为实参传入,所有的STL容器都提供insert()成员函数,这是唯一可以用于关联式容器身上的一种预定义inserter。
#include
#include
#include
#include
#include
using namespace std;
int main()
{
vector coll;
// 使用ctrl+z 回车进行终止
copy(istream_iterator(cin),
istream_iterator(),
back_inserter(coll));
sort(coll.begin(), coll.end());
unique_copy(coll.cbegin(), coll.cend(),
ostream_iterator(cout, "\n"));
}
#include
#include
#include
#include
#include
using namespace std;
int main()
{
vector coll;
for (int i = 1; i <= 9; i++) {
coll.push_back(i);
}
copy(coll.crbegin(), coll.crend(),
ostream_iterator(cout, " "));
}
输出:
9 8 7 6 5 4 3 2 1