string类中insert,find,copy的用法

下列网址提供了标准类库中string类的在线帮助:
http://www.cplusplus.com/reference/string/string/

http://en.cppreference.com/w/cpp/string

Capacity:
size
Return length of string (public member function )
length
Return length of string (public member function )
max_size
Return maximum size of string (public member function )
resize
Resize string (public member function )
capacity
Return size of allocated storage (public member function )
reserve
Request a change in capacity (public member function )
clear
Clear string (public member function )
empty
Test if string is empty (public member function )
shrink_to_fit
Shrink to fit (public member function )
通常来说,find函数用于寻找某个序列的在string中第一次出现的位置。
find函数有以下四种重载版本:

size_t find (const string& str, size_t pos = 0) const noexcept;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_type n) const;
size_t find (char c, size_t pos = 0) const noexcept;
参数说明:

str/s/c:要寻找的序列,可以是字符串(版本1),也可以是字符串字面值或者说C风格字符串(版本2、3,在版本3中,所寻找的序列是从s[0]开始的前n个字符),也可以是字符(版本4)。

pos:从string的pos位置开始寻找(注意第一个位置是0)。

函数返回序列第一次出现的位置,如果没有找到则返回string::npos。

样例:(摘自cplusplus.com)

#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;
}
// replacing in a string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string base="this is a test string.";
    string str2="n example";
    string str3="sample phrase";
    string str4="useful.";

    // replace signatures used in the same order as described above:

    // Using positions:                 0123456789*123456789*12345
    string str=base;           // "this is a test string."
    str.replace(9,5,str2);          // "this is an example string." (1)
    str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
    str.replace(8,10,"just a");     // "this is just a phrase."     (3)
    str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
    str.replace(22,1,3,’!’);        // "this is a short phrase!!!"  (5)
    cout << str << ’\n’;

}
// string::copy
#include <iostream>
#include <string>
using namespace std;
//int string::copy(char* s, int len, int pos = 0) const 函数原型
int main ()
{
  char ChangAn[20];
  string str ("Xi’an University of Post & Telecommunications");
  int length = str.copy(ChangAn,10,6);//发现一个小问题,应该是str.copy(Change,10,7)
  ChangAn[length]='\0';
  cout << "ChangAn contains: " << ChangAn << '\n';
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YULIU_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值