当前位置: 首页 > news >正文

建网站免费空间竞价托管哪家公司好

建网站免费空间,竞价托管哪家公司好,网站形象首页flash,网站建设方案模版目录 string 1. string的成员函数 1.1 构造、析构和赋值运算符重载 1.1.1 构造函数 1.1.2 析构函数 1.1.3 赋值运算符重载 1.2 迭代器 1.3 容量 1.4 元素访问 1.4.1 遍历方法 1.5 修改器 1.6 字符串操作 2. string的成员常量 3. string的非成员函数 string 以下…

目录

string

1. string的成员函数

1.1 构造、析构和赋值运算符重载

1.1.1 构造函数

1.1.2 析构函数

1.1.3 赋值运算符重载

1.2 迭代器

1.3 容量

1.4 元素访问

1.4.1 遍历方法

1.5 修改器

1.6 字符串操作

2. string的成员常量

3. string的非成员函数


string

以下函数加粗为重点。

1. string的成员函数

1.1 构造、析构和赋值运算符重载

1.1.1 构造函数

default

默认构造函数

string();构造空的string类对象,即空字符串

copy

拷贝构造函数

string(const string& str);构造一个str的拷贝

substring

string(const string& str, size_t pos, size_t len = npos);

用str中从pos开始的len个字符的子串来构造

如果str太短或者len是string::npos,则复制到str的末尾

from c-stringstring(const char* s);用C格式字符串来构造
from sequencestring(const char* s, size_t n);

用从s指向的字符串中前n个字符来构造

fillstring(size_t n, char c);用n个字符c来构造
rangetemplate <class InputIterator>   string(InputIterator first, InputIterator last);用范围[first,last]中的字符序列来构造
#include <string>
#include <iostream>using namespace std;int main()
{string s1;//defaultcout << s1 << endl;//空字符串string s2("hello world");//from c-stringcout << s2 << endl;//hello worldstring s3(s2);//copycout << s3 << endl;//hello worldstring s4(s3, 0, 5);//substringcout << s4 << endl;//hellostring s5("hello world", 5);//from sequencecout << s5 << endl;//hellostring s6(10, '*');//fillcout << s6 << endl;//**********return 0;
}

​​

1.1.2 析构函数

~string();销毁string类对象

1.1.3 赋值运算符重载

stringstring& operator=(const string& str);
c-stringstring& operator=(const char* s);
characterstring& operator=(char c);

1.2 迭代器

begin

&

end

iterator begin();
const_iterator begin() const;

iterator end();
const_iterator end() const;

begin返回一个迭代器,指向字符串的第一个字符

end返回一个迭代器,指向字符串的最后一个字符的下一个位置

rbegin

&

rend

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;

reverse_iterator rend();
const_reverse_iterator rend() const;

rbegin返回一个反向迭代器,指向字符串的最后一个字符

rend返回一个反向迭代器,指向字符串的第一个字符的上一个位置

cbegin

&

cend

const_iterator cbegin() const;
const_iterator cend() const;

cbegin返回一个const迭代器,指向字符串的第一个字符

cend返回一个const迭代器,指向字符串的最后一个字符的下一个位置

crbegin

&

crend

const_reverse_iterator crbegin() const;
const_reverse_iterator crend() const;

crbegin返回一个const反向迭代器,指向字符串的最后一个字符

crend返回一个const反向迭代器,指向字符串的第一个字符的上一个位置

begin&end和rbegin&rend返回的迭代器指向:

​​

const_iterator是一个指向const内容的迭代器。迭代器本身可以修改,但是它不能被用来修改它所指向的内容。

begin&end/rbegin&rend和cbegin&cend/crbegin&crend的不同:

  • begin&end/rbegin&rend的返回类型由对象是否是常量来决定。如果不是常量,返回iterator;如果是常量,返回const_iterator。
  • cbegin&cend/crbegin&crend的返回类型是const_iterator,不管对象本身是否是常量。
#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world");string::iterator it = s.begin();while (it != s.end()){cout << *it << " ";++it;}cout << endl;//h e l l o   w o r l dauto rit = s.rbegin();//string::reverse_iterator rit = s.rbegin();while (rit != s.rend()){cout << *rit << " ";++rit;}cout << endl;//d l r o w   o l l e hreturn 0;
}

1.3 容量

sizesize_t size() const;

返回字符串的长度

size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()

lengthsize_t length() const;返回字符串的长度
max_sizesize_t max_size() const;返回字符串能够达到的最大长度
resize

void resize(size_t n);

void resize(size_t n, char c);

调整字符串的长度为n(影响size)

·如果n<当前字符串的长度,多余的字符会被截掉

·如果n>当前字符串的长度,则:1)如果没有指定填充字符,则多出的空间用空字符填充;2)如果指定了填充字符c,则多出的空间用c填充

capacitysize_t capacity() const;返回分配的存储空间的大小
reservevoid reserve(size_t n = 0);

为字符串预留空间(影响capacity)

·如果n>当前的容量,容量增加到至少n个字符(空间大小满足16i-1)

·在所有其他情况下,它被视为缩小字符串容量的非约束性请求:容器实现可以自由地进行其他优化,并使字符串的容量大于n

clearvoid clear();清空字符串
emptybool empty() const;检测字符串是否为空串,是返回true,否则返回false
shrink_to_fitvoid shrink_to_fit();

收缩容量以适应字符串的长度

这个请求没有约束力,容器实现可以自由地进行优化,使字符串的容量大于其大小

#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world");cout << s.size() << endl;//11cout << s.length() << endl;//11cout << s.max_size() << endl;//2147483647cout << s.capacity() << endl;//15s.resize(15, '!');cout << s << endl;//hello world!!!!s.resize(13);cout << s << endl;//hello world!!s.reserve(30);cout << s.capacity() << endl;//31s.shrink_to_fit();cout << s.capacity() << endl;//15s.clear();if (s.empty())cout << "字符串为空" << endl;elsecout << "字符串不为空" << endl;//字符串为空return 0;
}

​​

1.4 元素访问

operator[]

char& operator[] (size_t pos);

const char& operator[] (size_t pos) const;

返回字符串中pos位置的字符的引用

没有越界检查

atchar& at(size_t pos);
const char& at(size_t pos) const;

返回字符串中pos位置的字符的引用

有越界检查,如果越界会抛异常

backchar& back();
const char& back() const;
返回字符串中最后一个字符的引用
frontchar& front();
const char& front() const;
返回字符串中第一个字符的引用
#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world");cout << s[0] << endl;//hcout << s.at(1) << endl;//ecout << s.back() << endl;//dcout << s.front() << endl;//hreturn 0;
}

1.4.1 遍历方法

1.4.1.1 operator[](最常用)

#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world");for (size_t i = 0; i < s.size(); i++){cout << s[i] << " ";}cout << endl;//h e l l o   w o r l dreturn 0;
}

1.4.1.2 迭代器

#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world");string::iterator it = s.begin();while (it != s.end()){cout << *it << " ";++it;}cout << endl;//h e l l o   w o r l dauto rit = s.rbegin();//string::reverse_iterator rit = s.rbegin();while (rit != s.rend()){cout << *rit << " ";++rit;}cout << endl;//d l r o w   o l l e hreturn 0;
}

1.4.1.3 范围for

#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world");for (auto ch : s){cout << ch << " ";}cout << endl;//h e l l o   w o r l dreturn 0;
}

1.5 修改器

operator+=string& operator+=(const string& str);
string& operator+=(const char* s);
string& operator+=(char c);
字符串追加
append

string& append(const string& str);

string& append(const string& str, size_t subpos, size_t sublen);
string& append(const char* s);

string& append(const char* s, size_t n);
string& append(size_t n, char c);

template <class InputIterator>   string& append(InputIterator first, InputIterator last);

字符串追加
push_backvoid push_back(char c);尾插(追加)一个字符
assign

string& assign(const string& str);

string& assign(const string& str, size_t subpos, size_t sublen);
string& assign(const char* s);

string& assign(const char* s, size_t n);
string& append(size_t n; char c);

template <class InputIterator>   string& assign(InputIterator first, InputIterator last);

给字符串赋值,替换其当前内容
insert

string& insert(size_t pos, const string& str);
string& insert(size_t pos, const string& str, size_t subpos, size_t sublen);
string& insert(size_t pos, const char* s);
string& insert(size_t pos, const char* s, size_t n);

string& insert(size_t pos, size_t n, char c);    void insert(iterator p, size_t n, char c);
iterator insert(iterator p, char c);
template <class InputIterator>   void insert(iterator p, InputIterator first, InputIterator last);

在字符串pos位置之前插入
erasestring& erase(size_t pos = 0, size_t len = npos);
iterator erase(iterator p);
iterator erase(iterator first, iterator last);
删除字符串的一部分
replacestring& replace(size_t pos, size_t len, const string& str); string& replace(iterator i1, iterator i2, const string& str);
string& replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
string & replace(size_t pos, size_t len, const char* s); string& replace(iterator i1, iterator i2, const char* s);
string& replace(size_t pos, size_t len, const char* s, size_t n); string& replace(iterator i1, iterator i2, const char* s, size_t n);
string& replace(size_t pos, size_t len, size_t n, char c); string& replace(iterator i1, iterator i2, size_t n, char c);
template <class InputIterator>  string& replace(iterator i1, iterator i2, InputIterator first, InputIterator last);
替换字符串的一部分
swapvoid swap(string& str);交换字符串的内容
pop_backvoid pop_back();尾删
#include <string>
#include <iostream>using namespace std;int main()
{string s1("hello");string s2("world");s1 += " ";s1 += s2;s1 += '!';cout << s1 << endl;//hello world!s2.append(" peace");s2.append(2, '!');cout << s2 << endl;//world peace!!//尾插一个字符的3种写法s1 += '*';//operator+=最常用s1.append(1, '~');s2.push_back('@');cout << s1 << endl;//hello world!*~cout << s2 << endl;//world peace!!@s1.assign("I want to study.");cout << s1 << endl;//I want to study.s1.insert(15, " and play");cout << s1 << endl;//I want to study and play.s1.erase(10, 10);cout << s1 << endl;//I want to play.s1.replace(0, 1, "They");cout << s1 << endl;//They want to play.s1.swap(s2);cout << s1 << endl;//world peace!!@cout << s2 << endl;//They want to play.s1.pop_back();cout << s1 << endl;//world peace!!return 0;
}

​​

把"hello world i love you"中的空格替换成"%20":

#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world i love you");/*//第1种方法 size_t num = 0;for (auto ch : s){if (ch == ' ')++num;//计算有几个空格}//提前开空间,避免repalce时扩容s.reserve(s.size() + 2 * num);size_t pos = s.find(' ');//第一个空格的位置while (pos != string::npos)//npos是长度参数,表示直到字符串结束{s.replace(pos, 1, "%20");pos = s.find(' ', pos + 3);}cout << s << endl;//hello%20world%20i%20love%20you*///第2种方法string newStr;size_t num = 0;for (auto ch : s){if (ch == ' ')++num;//计算有几个空格}//提前开空间,避免repalce时扩容newStr.reserve(s.size() + 2 * num);for (auto ch : s){if (ch != ' ')newStr += ch;elsenewStr += "%20";}s = newStr;cout << newStr << endl;//hello%20world%20i%20love%20youreturn 0;
}

1.6 字符串操作

c_strconst char* c_str() const;返回C格式字符串(以'\0'结尾)
dataconst char* data() const;

返回字符数组(不保证有‘\0’)

get_allocatorallocator_type get_allocator() const;返回配置器
copysize_t copy(char* s, size_t len, size_t pos = 0) const;

将对象中从pos开始的len个字符的子串复制到s指向的数组中(不会在复制内容的末尾附加'\0')

find

size_t find(const string & str, size_t pos = 0) const;
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;

从对象中pos位置开始往后匹配,返回第一次出现的位置
rfindsize_t rfind(const string & str, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos, size_t n) const;
size_t rfind(char c, size_t pos = npos) const;
从对象中pos位置开始往前匹配,返回第一次出现的位置
find_first_ofsize_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos, size_t n) const;
size_t find_first_of(char c, size_t pos = 0) const;

从对象中pos位置开始往后匹配,返回第一个与参数中指定的任何字符相匹配的字符的位置

find_last_ofsize_t find_last_of(const string& str, size_t pos = 0) const;
size_t find_last_of(const char* s, size_t pos = 0) const;
size_t find_last_of(const char* s, size_t pos, size_t n) const;
size_t find_last_of(char c, size_t pos = 0) const;
从对象中pos位置开始往前匹配,返回第一个与参数中指定的任何字符相匹配的字符的位置
find_first_not_ofsize_t find_first_not_of(const string& str, size_t pos = 0) const;
size_t find_first_not_of(const char* s, size_t pos = 0) const;
size_t find_first_not_of(const char* s, size_t pos, size_t n) const;
size_t find_first_not_of(char c, size_t pos = 0) const;
从对象中pos位置开始往后匹配,返回第一个与参数中指定的任何字符不匹配的字符的位置
find_last_not_ofsize_t find_last_not_of(const string& str, size_t pos = 0) const;
size_t find_last_not_of(const char* s, size_t pos = 0) const;
size_t find_last_not_of(const char* s, size_t pos, size_t n) const;
size_t find_last_not_of(char c, size_t pos = 0) const;
从对象中pos位置开始往前匹配,返回第一个与参数中指定的任何字符不匹配的字符的位置
substrstring substr(size_t pos = 0, size_t len = npos) const;

返回一个从pos开始的len个字符的子串

compareint compare(const string& str) const;
int compare(size_t pos, size_t len, const string& str) const; int compare(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
int compare(const char* s) const; int compare(size_t pos, size_t len, const char* s) const;
int compare(size_t pos, size_t len, const char* s, size_t n) const;

比较字符串

返回值=0,对象=比较字符串

返回值<0,对象<比较字符串

返回值>0,对象>比较字符串

#include <string>
#include <iostream>using namespace std;int main()
{string s("hello");cout << s.c_str() << endl;//hellocout << (void*)s.c_str() << endl;//地址cout << s.data() << endl;//hellochar buffer[10] = { '\0' };s.copy(buffer, 3, 1);for (auto ch : buffer){cout << ch;}cout << endl;//ellsize_t pos = s.find('l');while (pos != string::npos)//npos是长度参数,表示直到字符串结束{s.replace(pos, 1, "L");pos = s.find('l', pos + 1);}cout << s << endl;//heLLopos = s.rfind('L');while (pos != string::npos){s.replace(pos, 1, "l");pos = s.rfind('L', pos - 1);}cout << s << endl;//hellocout << s.find_first_of("jklmn") << endl;//2cout << s.find_last_of("jklmn") << endl;//3cout << s.find_first_not_of("heL") << endl;//2cout << s.find_last_not_of("Lo") << endl;//3cout << s.substr(1, 3) << endl;//ellcout << s.compare("hello world") << endl;//-1return 0;
}

2. string的成员常量

npos为长度参数,表示直到字符串结束。

把"hello world i love you"中的空格替换成"%20":

#include <string>
#include <iostream>using namespace std;int main()
{string s("hello world i love you");//第1种方法size_t num = 0;for (auto ch : s){if (ch == ' ')++num;//计算有几个空格}//提前开空间,避免repalce时扩容s.reserve(s.size() + 2 * num);size_t pos = s.find(' ');//第一个空格的位置while (pos != string::npos)//npos是长度参数,表示直到字符串结束{s.replace(pos, 1, "%20");pos = s.find(' ', pos + 3);}cout << s << endl;//hello%20world%20i%20love%20you/*//第2种方法string newStr;size_t num = 0;for (auto ch : s){if (ch == ' ')++num;//计算有几个空格}//提前开空间,避免repalce时扩容newStr.reserve(s.size() + 2 * num);for (auto ch : s){if (ch != ' ')newStr += ch;elsenewStr += "%20";}s = newStr;cout << newStr << endl;//hello%20world%20i%20love%20you*/return 0;
}

3. string的非成员函数

operatpr+string operator+ (const string& lhs, const string& rhs);
string operator+ (const string & lhs, const char* rhs); string operator+ (const char* lhs, const string& rhs);
string operator+ (const string& lhs, char rhs); string operator+ (char lhs, const string& rhs);

+运算符重载

(尽量少用,因为传值返回,导致深拷贝效率低)

relational operatorsbool operator== (const string& lhs, const string& rhs); bool operator== (const char* lhs, const string& rhs); bool operator== (const string& lhs, const char* rhs);
bool operator!= (const string& lhs, const string& rhs); bool operator!= (const char* lhs, const string& rhs); bool operator!= (const string& lhs, const char* rhs);
bool operator<  (const string& lhs, const string& rhs); bool operator<  (const char* lhs, const string& rhs); bool operator<  (const string& lhs, const char* rhs);
bool operator<= (const string& lhs, const string& rhs); bool operator<= (const char* lhs, const string& rhs); bool operator<= (const string& lhs, const char* rhs);
bool operator>  (const string& lhs, const string& rhs); bool operator>  (const char* lhs, const string& rhs); bool operator>  (const string& lhs, const char* rhs);
bool operator>= (const string& lhs, const string& rhs); bool operator>= (const char* lhs, const string& rhs); bool operator>= (const string& lhs, const char* rhs);
关系运算符重载
swapvoid swap(string& x, string& y);交换两个字符串
operator>>istream& operator>>(istream& is, string& str);>>运算符重载
operator<<ostream& operator<<(ostream& os, const string& str);<<运算符重载
getlineistream& getline(istream& is, string& str, char delim);
istream& getline(istream& is, string& str);
从流中获取一行到字符串
http://www.hkea.cn/news/598056/

相关文章:

  • 上海市住房城乡建设管理委员会网站网络营销软文范例大全800
  • 莱芜区政协网站做网络优化的公司排名
  • 太原网站建设开发公司电商运营基本知识
  • php做企业网站seo网站推广企业
  • 万网网站备案授权书免费发布推广信息的b2b
  • 乡镇可以做门户网站seo是什么意思职业
  • 建设银行网站优点做个公司网站大概多少钱
  • 网站标题的设置方法哪家建设公司网站
  • 网站空间托管电商平台的营销方式
  • 网站制作专业的公司有哪些seo网站编辑是做什么的
  • wordpress 分栏seo怎么优化简述
  • php网站开发 多少钱推广方案策划
  • 芜湖做网站公司广州seo好找工作吗
  • 做网站找客户百度竞价推广公司
  • 深圳网站建设怎么办互联网营销的优势
  • 课程网站开发背景网站推广的几种方法
  • 商城网站建设模板一份完整的营销策划方案
  • 推广网站建设网站权重查询工具
  • t型布局网站怎么做建设网官方网站
  • 哪个建设网站推广竞价托管公司
  • 网站建设傲seo网站是什么意思
  • 卢氏住房和城乡建设厅网站聚名网
  • 山东网站建设电话长沙靠谱的关键词优化
  • 山东营销型网站ip网站查询服务器
  • 什么网站上做奥数题企业培训课程清单
  • 龙岩优化seo是什么意思
  • 外贸网站建设源码软文怎么做
  • 文章列表页wordpress宁波seo资源
  • 获取网站访客qq 原理百度投诉中心人工电话
  • 企业网站制作查询百度电话怎么转人工