C++ 方法

string截取字符串:

stringvar.substr(start , [length ])

参数 stringvar   必选项。要提取子字符串的字符串文字或 String 对象。

start 必选项。所需的子字符串的起始位置。字符串中的第一个字符的索引为 0。       

length  可选项。在返回的子字符串中应包括的字符个数。 如果 length 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到 stringvar 的最后。

string s = "0123456789"; 

string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = "56789" 

string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = "567" 

vector的find查找:

InputIterator find (InputIterator first, InputIterator last, const T& val);

find(vec.begin(), vec.end(), val), 在vec中找val。若== -1,则说明未找到,即等同于不存在于vec中。

// find example
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector

int main () {
  // using std::find with array and pointer:
  int myints[] = { 10, 20, 30, 40 };
  int * p;

  p = std::find (myints, myints+4, 30);
  if (p != myints+4)
    std::cout << "Element found in myints: " << *p << '\n';
  else
    std::cout << "Element not found in myints\n";

  // using std::find with vector and iterator:
  std::vector<int> myvector (myints,myints+4);
  std::vector<int>::iterator it;

  it = find (myvector.begin(), myvector.end(), 30);
  if (it != myvector.end())
    std::cout << "Element found in myvector: " << *it << '\n';
  else
    std::cout << "Element not found in myvector\n";

  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38628152/article/details/82820967