STL 笔记: deque vector string

来源:互联网 发布:炫踪网络李化亮 编辑:程序博客网 时间:2024/05/16 08:43

英文描述参考: www.cplusplus.com 部分解释参考:stackoverflow

std deque 和 vector

They provide a functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations: accessing elements in a deque by offsetting a pointer to another element causes undefined behavior.

deque和vector非常相似,不同点在于deque支持在头尾插入和删除,而且在两端插入和删除不会导致重新分配空间。vector只能在末尾实现插入和删除。deque不保证连续性存储空间,指针偏置可能导致获取不到元素。根据以上性质可知,deque除了有push_back,pop_back还有push_front,pop_front。vector::size()和deque::size()都返回size_type类型的数据。vector::front()和vector::begin()的区别在于前者是reference 而后者是iterator。

下面这里例子说明了deque分配内存时是不连续的,如果声明std::vector myvector; 那么&*it&*(it-1)相差4(int),如果声明std::deque myvector; 那么&*it&*(it-1)不一定相差4。

#include <iostream>#include <vector>#include <deque>int main (){  std::deque<int> myvector;  std::deque<int>::iterator it;  // set some content in the vector:  for (int i=0; i<128; i++) myvector.push_back(i);  std::cout << "size: " << (int) myvector.size() << '\n';  it=myvector.end();  std::cout << "it: " << &*it << '\n';  it--;  std::cout << "it: " << &*it << '\n';  std::cout << "max_size: " << (int) myvector.max_size() << '\n';  return 0;}

deque::push_front的例子

// deque::push_front#include <iostream>#include <deque>int main (){  std::deque<int> mydeque (2,100);     // two ints with a value of 100  mydeque.push_front (200);  mydeque.push_front (300);  std::cout << "mydeque contains:";  for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}

vector::front的例子

// vector::front#include <iostream>#include <vector>int main (){  std::vector<int> myvector;  myvector.push_back(78);  myvector.push_back(16);  // now front equals 78, and back 16  myvector.front() -= myvector.back();  std::cout << "myvector.front() is now " << myvector.front() << '\n';  // myvector.front() is now 62  return 0;}

std string

substr

string::substr(size_t pos = 0, size_t len = npos) 第一个参数是提取的位置,第二个参数时提取的长度。对string进行操作不会改变string。

// string::substr#include <iostream>#include <string>int main (){  std::string str="We think in generalities, but we live in details.";  std::string str2 = str.substr (3,5);     // "think"  std::size_t pos = str.find("live");      // position of "live" in str  std::string str3 = str.substr (pos);     // get from "live" to the end  std::cout << str2 << ' ' << str3 << '\n';  return 0;}// OUTPUT: think live in details.

find

函数的四种形式:
string (1) size_t find (const string& str, size_t pos = 0) const;
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_t n) const;
character (4) size_t find (char c, size_t pos = 0) const;

pos: Position of the first character in the string to be considered in the search. str寻找的开始位置,最大为npos。
n: Length of sequence of characters to match. 要匹配的长度
found=str.find(“needles are small”,found+1,6); 在str”found+1”的位置开始寻找,匹配”needles are small”前6个字符。

// string::find#include <iostream>       // std::cout#include <string>         // std::stringint 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;}

rfind

size_t rfind (const string& str, size_t pos = npos) const;
Searches the string for the last occurrence of the sequence specified by its arguments. 在string中找最后一次出现的位置
pos: Position of the last character in the string to be considered as the beginning of a match. str寻找的结束位置,最大为npos。(此处和find()函数不同)

// string::rfind#include <iostream>#include <string>#include <cstddef>int main (){  std::string str ("The sixth sick sheik's sixth sheep's sick.");  std::string key ("sixth");  std::size_t found = str.rfind(key);  if (found!=std::string::npos)    str.replace (found,key.length(),"seventh");  std::cout << str << '\n';  // The sixth sick sheik's seventh sheep's sick.  return 0;}

find_first_of

Searches the string for the first character that matches any of the characters specified in its arguments. 顺序查找string中任何一个匹配str中的字符
下面的例子是顺序查找元音字母,并替换成’*’。

// string::find_first_of#include <iostream>       // std::cout#include <string>         // std::string#include <cstddef>        // std::size_tint main (){  std::string str ("Please, replace the vowels in this sentence by asterisks.");  std::size_t found = str.find_first_of("aeiou");  while (found!=std::string::npos)  {    str[found]='*';    found=str.find_first_of("aeiou",found+1);  }  std::cout << str << '\n';  return 0;}

find_last_of

和find_first_of类似,逆序查找string中任何一个匹配str中的字符。

// string::find_last_of#include <iostream>       // std::cout#include <string>         // std::string#include <cstddef>         // std::size_tvoid SplitFilename (const std::string& str){  std::cout << "Splitting: " << str << '\n';  std::size_t found = str.find_last_of("/\\");  std::cout << " path: " << str.substr(0,found) << '\n';  std::cout << " file: " << str.substr(found+1) << '\n';}int main (){  std::string str1 ("/usr/bin/man");  std::string str2 ("c:\\windows\\winhelp.exe");  SplitFilename (str1);  SplitFilename (str2);  return 0;}
0 0