C++库函数笔记
1.qsort函数、sort函数
先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的。
首先说一下, qsort
qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有多个重复值的数组来说,基本快速排序的效率较低,且不稳定)。集成在C语言库函数里面的的qsort函数,使用 三 路划分的方法解决排序这个问题。所谓三路划分,是指把数组划分成小于划分值,等于划分值和大于划分值的三个部分。
具体介绍:-^^
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )
int compare (const void *elem1, const void *elem2 ) );
qsort(即,quicksort)主要根据你给的比较条件给一个快速排序,主要是通过指针移动实现排序功能。排序之后的结果仍然放在原来数组中。
参数意义如下:
第一个参数 base 是 需要排序的目标数组名(或者也可以理解成开始排序的地址,因为可以写&s[i]这样的表达式)
第二个参数 num 是 参与排序的目标数组元素个数
第三个参数 width 是单个元素的大小(或者目标数组中每一个元素长度),推荐使用sizeof(s[0])这样的表达式
第四个参数 compare 就是让很多人觉得非常困惑的比较函数啦。
我们来简单讨论compare这个比较函数(写成compare是我的个人喜好,你可以随便写成什么,比如 cmp 什么的,在后面我会一直用cmp做解释)。 典型的compare的定义是int compare(const void *a,const void *b);
返回值必须是int,两个参数的类型必须都是const void *,那个a,b是随便写的,个人喜好。假设是对int排序的话,如果是升序,那么就是如果a比b大返回一个正值,小则负值,相等返回0,其他的依次类推,后面有例子来说明对不同的类型如何进行排序。
qsort 的使用方法:
一、对int类型数组排序
int num[100]; int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; //升序排序 //return *(int *)b - *(int *)a; //降序排序 /*可见:参数列表是两个空指针,现在他要去指向你的数组元素。所以转型为你当前的类型,然后取值。 升序排列时,若第一个参数指针指向的“值”大于第二个参数指针指向的“值”,则返回正;若第一个参数指针指向的“值”等于第二个参数指针指向的“值”,则返回零;若第一个参数指针指向的“值”小于第二个参数指针指向的“值”,则返回负。 降序排列时,则刚好相反。 */ } qsort(s,n,sizeof(s[0]),cmp);
sort 使用时得注明:using namespace std; 或直接打 std::sort() 还得加上 #include <algorithm> 头文件
sort模板函数有两种输入方式:
default (1) template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
其中comp为对比函数指针,返回bool类型,first和last可以是迭代器也可以是数组指针。
Sorts the elements in the range [first,last) into ascending order.
The elements are compared using operator< for the first version, and comp for the second.
Equivalent elements are not guaranteed to keep their original relative order (see stable_sort).
2.find函数
用于在容器或数组中寻找元素值为val的地址,若无对应值则返回last。要#include <algorithm>
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
InputIterator find (InputIterator first, InputIterator last, const T& val);
Find value in range
Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.
The function uses operator== to compare the individual elements to val.
3.四舍五入函数
#include <math.h> double round (double x); float roundf (float x); long double roundl (long double x);
Returns the integral value that is nearest to x, with halfway cases rounded away from zero.
将结果在进行类型转换可得到四舍五入后的整数值。
4.数值转换为字符串函数
string to_string (int val); string to_string (long val); string to_string (long long val); string to_string (unsigned val); string to_string (unsigned long val); string to_string (unsigned long long val); string to_string (float val); string to_string (double val); string to_string (long double val);
注意负号也会被转换为字符
5.string::find()
|
string (1)
|
size_t find (const string& str, size_t pos = 0) const noexcept;
|
|
c-string (2)
|
size_t find (const char* s, size_t pos = 0) const;
|
|
buffer (3)
|
size_t find (const char* s, size_t pos, size_type n) const;
|
|
character (4)
|
size_t find (char c, size_t pos = 0) const noexcept;
|
Find content in string
Searches the string for the first occurrence of the sequence specified by its arguments.
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that just one of these characters match, but the entire sequence must match.
Parameters
str
Another string with the subject to search for.
pos
Position of the first character in the string to be considered in the search.
If this is greater than the string length, the function never finds matches.
Note: The first character is denoted by a value of 0 (not 1): A value of 0 means that the entire string is searched.
s
Pointer to an array of characters.
If argument n is specified (3), the sequence to match are the first n characters in the array.
Otherwise (2), a null-terminated sequence is expected: the length of the sequence to match is determined by the first occurrence of a null character.
n
Length of sequence of characters to match.
c
Individual character to be searched for.
size_t is an unsigned integral type (the same as member type string::size_type).
Return Value
The position of the first character of the first match.
If no matches were found, the function returns string::npos.
size_t is an unsigned integral type (the same as member type string::size_type).
Example
// string::find #include <iostream> // std::cout #include <string> // std::string int main () { std::string str ("There are two needles in this haystack with needles."); std::string str2 ("needle"); // different member versions of find in the same order as above: std::size_t found = str.find(str2); if (found!=std::string::npos) std::cout << "first 'needle' found at: " << found << '\n'; found=str.find("needles are small",found+1,6); if (found!=std::string::npos) std::cout << "second 'needle' found at: " << found << '\n'; found=str.find("haystack"); if (found!=std::string::npos) std::cout << "'haystack' also found at: " << found << '\n'; found=str.find('.'); if (found!=std::string::npos) std::cout << "Period found at: " << found << '\n'; // let's replace the first needle: str.replace(str.find(str2),str2.length(),"preposition"); std::cout << str << '\n'; return 0; }
6.将string中的字符进行转换
#include <algorithm>//将string中的字符进行转换 transform(str.begin(), str.end(), str.begin(), (int(*)(int)) tolower);
判断字符是数字或是字母(大小写也可以判断)
#include <cctype> isdigit(char c) isalpha(c)相当于isupper(c)||islower(c)
在string中查找指定字符
size_t find (char c, size_t pos = 0) const noexcept;
Find character in string (public member function )
size_t find_first_of (char c, size_t pos = 0) const;
Find character in string from the end (public member function )
size_t find_last_of (char c, size_t pos = npos) const noexcept;
若未查找到,返回returns string::npos.
构造字符串的子串
substr
Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).
string substr (size_t pos = 0, size_t len = npos) const;
len:
Number of characters to include in the substring (if the string is shorter, as many characters as possible are used).
A value of string::npos indicates all characters until the end of the string.
7.count统计函数
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val);
Count appearances of value in range
Returns the number of elements in the range [first,last) that compare equal to val.
The function uses operator== to compare the individual elements to val.
Parameters
first, last
Input iterators to the initial and final positions of the sequence of elements. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
val
Value to match.
T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side).
Return value
The number of elements in the range [first,last) that compare equal to val.
The return type (iterator_traits<InputIterator>::difference_type) is a signed integral type.
string可以直接用==号判断是否全数组相等
如果vector里面的元素类型是简单类型(内置类型),可以直接使用“==”或者“!=”进行比较
甚至可以使用“<=” “<” “>=” “>”比较两个vector大小:按照字典序排列

浙公网安备 33010602011771号