函数名称 | 功能说明 |
string() |
构造空的
string
类对象,即空字符串
|
string(const char* s)
|
用
C-string
来构造
string
类对象
|
string(size_t n, char c)
|
string
类对象中包含
n
个字符
c
|
string(const string&s)
|
拷贝构造函数
|
void test1()
{
string s1; // 构造空的string类对象s1
string s2("hello world"); // 用C格式字符串构造string类对象s2
string s3(s2); // 拷贝构造s3
}
函数名称
|
功能说明
|
size()
|
返回字符串有效字符长度
|
ength()? ? ?
| 返回字符串有效字符长度(和size()作用一样) |
capacity
|
返回空间总大小
|
empty
|
检测字符串释放为空串,是返回
true
,否则返回
false
|
clear
|
清空有效字符
|
reserve
|
为字符串预留空间
|
resize
|
将有效字符的个数该成
n
个,多出的空间用字符
c
填充
|
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2("hello world");
//reserve,只会扩容,不会改变字符的数据
cout << s1.size() << endl;
cout << s1.capacity() << endl;
cout << s2.size() << endl;
cout << s2.capacity() << endl;
s1.reserve(100);
cout << s1.capacity() << endl;
s2.reserve(100);
cout << s2.capacity() << endl;
//resize,——改变字符的大小,会扩容,也可以改变字符的数据
s1.resize(100, 'x');
cout << s1.size() << endl;
cout<<s1.capacity() << endl;
cout << s1 << endl;
s2.resize(100, 'x');
cout << s2.size() << endl;
cout << s2.capacity() << endl;
cout << s2 << endl;
s2.resize(5, 'x');//当resize中的参数n比实际size要小,只会保留前n的数据内容
cout << s2.capacity() << endl;
cout << s2;
return 0;
}
函数名称
|
功能说明
|
operator[]
|
返回
pos
位置的字符,
const string
类对象调用(如果越界会报错)
|
begin
+
end
|
begin
获取一个字符的迭代器
+
end
获取最后一个字符下一个位置的迭
代器
|
rbegin
+
rend
|
begin
获取一个字符的迭代器
+
end
获取最后一个字符下一个位置的迭
代器
|
范围
for
|
C++11
支持更简洁的范围
for
的新遍历方式
|
int main()
{
string s1 ="hello world";
cout << s1 <<endl;
cout << s1.size();
for (size_t i = 0; i < s1.size(); i++)//[]:可以访问也可以对它进行修改;
{
cout << s1[i] <<" ";
}
cout << endl;
for (size_t i = 0; i < s1.size(); i++)//[]:可以访问也可以对它进行修改;
{
s1[i] = '0';
cout << s1[i] << " ";
}
//把s1逆置;
//(1)第一种方法;
size_t begin = 0, end = s1.size() - 1;
while (begin < end)
{
char tmp = s1[begin];
s1[begin] = s1[end];
s1[end] = tmp;
begin++;
end--;
}
cout << s1 << endl;
cout << "--------------------------------------------------------------------------------" << endl;
//第二种
while (begin<end)
{
swap(s1[begin],s1[end]);
begin++;
end--;
}
cout << s1 << endl;
cout << "--------------------------------------------------------------------------------" << endl;
//第三种:迭代器iterator
string::iterator it = s1.begin();
while (it!= s1.end())
{
cout << *it ;
++it;
}
cout << "--------------------------------------------------------------------------------" << endl;
//第三种:迭代器iterator
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it;
++it;
}
cout << "--------------------------------------------------------------------------------" << endl;
reverse(s1.begin(), s1.end());//
cout << s1;
return 0;
}
函数名称
|
功能说明
|
push_back
|
在字符串后尾插字符
c
|
append
|
在字符串后追加一个字符串
|
operator+=
|
在字符串后追加字符串
str
|
c_str
|
返回
C
格式字符串
|
find
+
npos
|
从字符串
pos
位置开始往后找字符
c
,返回该字符在字符串中的位置
|
rfind
|
从字符串
pos
位置开始往前找字符
c
,返回该字符在字符串中的位置
|
substr
|
在
str
中从
pos
位置开始,截取
n
个字符,然后将其返回
|
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
string s2("HELLO");
s1.push_back('x');//push_back尾插字符;
s1.erase(11, 1);
s1.append(s2);//尾插字符数组
s1.append("HELLO", 2);//尾插字符串的前两个字符
s1.append(++s2.begin(),--s2.end() );//对字符数组s2,可以取任意区间尾插;
s1 += s2;
s1.insert(1,s2);//在某个位置头插
cout << s1 << endl;
return 0;
}
函数
|
功能说明
|
operator+
|
尽量少用,因为传值返回,导致深拷贝效率低
|
operator>>
|
输入运算符重载
|
operator<<
|
输出运算符重载
|
getline
|
获取一行字符串
|
relational operators
|
大小比较
|
为了方便测试,这里是分文件写的,这个是string.h文件
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
namespace A
{
typedef char* iterator;
class string
{
public:
string(const char* str = "")//构造函数
{
_size = strlen(str);
_capacity = _size;
_str = new char[_size + 1];
strcpy(_str, str);
}
~string()//析构函数
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
string(const string& str)//构造函数
{
_str = new char[str._capacity + 1];
strcpy(_str, str._str);
_size = str._size;
_capacity = str._capacity;
}
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
const char* c_str()const
{
return _str;
}
const size_t size()const
{
return _size;
}
char& operator[](size_t pos)
{
return _str[pos];
}
const char& operator[](size_t pos)const
{
return _str[pos];
}
iterator begin()
{
return _str;
}
const iterator begin()const
{
return _str;
}
iterator end()
{
return _str + _size;
}
const iterator end()const
{
return _str + _size;
}
size_t find(const char* str, size_t pos = 0)
{
const char* ptr = strstr(_str + pos, str);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr - _str;
}
}
void reserve(size_t n = 0)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[]_str;
_str = tmp;
_capacity = n;
}
}
void clear()
{
_size = 0;
_str[0] = '\0';
}
void push_back(char c)
{
if (_size == _capacity)
{
size_t new_capacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(new_capacity);
}
_str[_size] = c;
_size++;
_str[_size] = '\0';
}
void append(const char* s)
{
size_t len = strlen(s);
size_t end = _size + len;
if (end > _capacity)
{
size_t new_capacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(new_capacity);
}
strcpy(_str + _size, s);
_size += len;
_str[_size] = '\0';
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* s)
{
size_t len = strlen(s);
if (_size + len > _capacity)
{
size_t new_capacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(new_capacity);
}
append(s);
return *this;
}
void erase(size_t pos = 0, size_t len = npos)
{
if (len == npos || len > _size - pos)
{
for (size_t i = pos; i < _size; i++)
{
_str[i] = '\0';
}
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void insert(size_t pos, const char* s)
{
size_t len =strlen(s);
if (_size + len > _capacity)
{
size_t new_capacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(new_capacity);
}
int end = _size;
while (end >= (int)pos)
{
_str[end+len] = _str[end];
end--;
}
strncpy(_str + pos, s, len);
_size += len;
}
size_t find(const string& str, size_t pos = 0)
{
char* ptr = strstr(_str + pos, str._str);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr-_str;
}
}
string substr(size_t pos = 0, size_t len = npos)
{
string s;
size_t end = pos + len;
if (len == npos || len >= _size - pos)
{
end = _size;
}
reserve(end - pos);
for (size_t i = pos; i < end; i++)
{
s += _str[i];
}
return s;
}
private:
char* _str;
size_t _size;
size_t _capacity;
static size_t npos;
};
ostream& operator<<( ostream& out,const string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s)
{
char ch;
ch = in.get();
while (ch != '\n' && ch != ' ')
{
s += ch;
ch = in.get();
}
return in;
}
size_t string::npos = -1;
void Test1()
{
string s1("hello world");
cout << s1.c_str() << endl;
cout << s1[6] << endl;
string s2;
cout << s2.c_str() << endl;
}
void test2()
{
string s1("hello world");
string s2(s1);
cout << s2.c_str() << endl;
string s3("hhhhhhhhhhhhhhhhhhhh");
s1 = s3;
cout << s1.c_str() << endl;
}
void test3()
{
string s1("hello world");
string s2;
s1.push_back('c');
cout << s1.c_str() << endl;
s1.append("abcdefg");
cout << s1.c_str()<< endl;
s1 += "jjjjjjjjj";
s1.erase(5,3);
s1.insert(0, "xxx");
cout << s1.c_str() << endl;
cout << s1.find(s2) << endl;
string s3 = s1.substr(6,4);
cout << s3.c_str() << endl;
cout << s1 << endl;
cin >> s2;
cout << s2 << endl;
}
}
void test4()
{
std::string s1("1234");
int i = stoi(s1);
cout << i;
string s2 = to_string(123);
cout << s2;
}
下面是test.cpp文件
#include"string.h"
int main()
{
//A::Test1();
//A::test2();
//A::test3();
test4();
return 0;
}