std::strlen//注意strlen与sizeof的区别 std::size_tstrlen( constchar* str ); Parameters: str - pointer to the null-terminated byte string to be examined Return value: The length of the null-terminated string str.
std::strcmp intstrcmp( constchar *lhs, constchar *rhs ); Parameters: lhs, rhs - pointers to the null-terminated byte strings to compare Return value: Negative value if lhs appears before rhs in lexicographical order. //小于 Zero if lhs and rhs compare equal. //等于 Positive value if lhs appears after rhs in lexicographical order. //大于
std::strcpy char* strcpy( char* dest, constchar* src ); Parameters dest - pointer to the character array to write to src - pointer to the null-terminated byte string to copy from Return value dest
std::memset void* memset( void* dest, int ch, std::size_t count ); Parameters dest - pointer to the object to fill ch - fill byte count - number of bytes to fill Return value dest
std::memcpy void* memcpy( void* dest, constvoid* src, std::size_t count ); Parameters dest - pointer to the memory location to copy to src - pointer to the memory location to copy from count - number of bytes to copy Return value dest
<ctime> time(); /*-----From C++ reference-----*/ #include<ctime> #include<iostream>
intmain() { std::time_t result = std::time(nullptr); std::cout << std::asctime(std::localtime(&result)) << result << " seconds since the Epoch\n"; }
OUTPUT: Wed Sep 2110:27:522011 1316615272 seconds since the Epoch /*----------------------------*/ 还可配合随机数使用: /*-----From C++ reference-----*/ #include<cstdlib> #include<iostream> #include<ctime>
intmain() { std::srand(std::time(0)); //use current time as seed for random generator int random_variable = std::rand(); std::cout << "Random value on [0 " << RAND_MAX << "]: " << random_variable << '\n'; }
Possible output: Random value on [02147483647]: 1373858591 /*----------------------------*/
<stack> //栈:后进先出 stack<int> sta; sta.push(); //inserts element at the top int topElement = sta.top(); //accesses the top element sta.pop(); //removes the top element sta.empty(); //checks whether the underlying container is empty sta.size(); //returns the number of elements //入、出、获取栈顶元素 O(1)
<queue> //队列:先进先出 queue<int> que; que.push(); //inserts element at the end int frontElement = que.front(); //access the first element int backElement = que.back(); //access the last element que.pop(); //removes the first element que.empty(); //checks whether the underlying container is empty que.size(); //returns the number of elements //入、出、获取队列元素 O(1)
set<int> st; // 集合 st.clear(); // set 的清空 st.insert(); // 插入一个元素,自带去重 // 查询有无元素 x int hav = s.count(x); // 返回 0 或 1 st.find(); // 查找一个元素并返回迭代器 set<int>::iterator it = st.find(x); st.erase(); // 删除元素 st.size(); // 求集合元素个数 st.empty(); // 判断是否为空集
set 用来保存很多很多元素,并能够在 O(logn) 的时间内查找、删除、添加某个元素,效率非常高
set 最主要的用途:自动去重并按升序排序
set 只能通过迭代器(iterator)访问
迭代器的 ++ 和 – 能够在 O(logn) 的时间里找到第一个比它大(小)的数
set 的遍历
1 2 3 4 5 6
// 注意,set 不支持 it < st.end() 的写法,而 vector 支持 // 原因是 vector 是一维的,在地址上是连续的,而 set 是堆结构,在地址上不连续 // 统一写 it != elem.end() 就好啦 // for (auto it = st.begin(); it != st.end(); it++) for (set<int>::iterator it = st.begin(); it != st.end(); it++) printf("%d ", *it);