【C++】第十节—string类(完结篇)——详解+代码示例
hello ,好久不见!云边有个稻草人-个人主页
C++优质专栏!~接待订阅~
目录
一、string实现时要注意的细节点
1.void string::Insert(size_t pos, char ch)
2.void string::Insert(size_t pos, const char* str)
第一种方法
第二种方法
3.到底在哪里定义static const size_t npos ?
4.实现取域名
5.拷贝—拷贝构造和赋值
6.流插入
7.三个swap
二、
2.1 经典的string类题目
2.2 浅拷贝
2.3 深拷贝
(1)【传统版写法的String类】
(2)【现代版写法的String类】
(3)【写时拷贝】(了解)
(4)扩展阅读
(5)编码
三、综合代码
string.h
string.cpp
test.cpp
正文开始——
一、string实现时要注意的细节点
1.void string::Insert(size_t pos, char ch)
https://i-blog.csdnimg.cn/direct/144e4fbf57a040a68e2b71c00cdcfd96.png
void string::Insert(size_t pos, char ch)
{
//对于任何的插入操作都要先判断空间是否充足提前预留足够的空间
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
size_t end = _size+1;
//预留出pos位置的空间进行插入操作
while (end > pos)
{
_str = _str;
--end;
}
_str = ch;
_size++;
} 2.void string::Insert(size_t pos, const char* str)
第一种方法
跟我们前面讲的很相似,使用强转来解决范例的题目
void string::Insert(size_t pos, const char* str)
{
//还是先扩容
size_t len = strlen(str);
if (_size + len > _capacity)
{
//扩容
size_t newCapacity = 2 * _capacity;
//扩2倍不够直接需要多少扩多少
if (newCapacity < _size + len)
newCapacity = _size + len;
reserve(newCapacity);
}
int end = _size;
while (end >= (int)pos)
{
_str = _str;
end--;
}
//将str进行挨个插入
for (size_t i = 0; i < len; i++)
{
_str = str;
}
_size += len;
} 第二种方法
是将end-len移到end指向的数据
https://i-blog.csdnimg.cn/direct/210d3d2e552040a3957ab9e5a2faa370.png
void string::Insert(size_t pos, const char* str)
{
//还是先扩容
size_t len = strlen(str);
if (_size + len > _capacity)
{
//扩容
size_t newCapacity = 2 * _capacity;
//扩2倍不够直接需要多少扩多少
if (newCapacity < _size + len)
newCapacity = _size + len;
reserve(newCapacity);
}
size_t end = _size + len;
while (end >= pos+len) //end-len==pos,end==pos+len,
{
_str = _str;
end--;
}
//将str进行挨个插入
for (size_t i = 0; i < len; i++)
{
_str = str;
}
_size += len;
} 3.到底在哪里定义static const size_t npos ?
(1)所有的.c或者.cpp文件在编译链接的时候都会生成.o文件(VS下叫.obj文件),.h文件在预处理的时候就在两个.cpp文件里面展开了,我们在类域外面定义的npos就会在两个.cpp里面展开,当两个.cpp文件链接合并生成可执行步伐的时候,就构成了下面报错里面的重定义,以是当静态成员变量定义和声明分离的时候要在.cpp文件里面定义,不能像以前那样在类里面声明在类外面定义。对于npos要加上const,不可修改。(2)同时npos要在定义的时候给值,对于const对象是只有定义时一次初始化的时机。(3)特殊的一点,对于静态的const(只有整型范例的数据才可以的,这算是一个特殊处理)成员变量可以在声明的时候给缺省值,那么在定义的时候就不可以再给值了。
https://i-blog.csdnimg.cn/direct/7f9a7f3b8fd0456cbdbbba4f133095db.png
https://i-blog.csdnimg.cn/direct/0e824bc5adeb4845abecb240aa1fe04a.png
总结一下,对于这种环境有两种解决办法
https://i-blog.csdnimg.cn/direct/e693145fefe74a2893747898c1f556c7.png
https://i-blog.csdnimg.cn/direct/4756c679aef74b068e6afcc10397fddc.png
https://i-blog.csdnimg.cn/direct/012d75bdea3c40c4993aba15b6547839.png
4.实现取域名
https://i-blog.csdnimg.cn/direct/0b39df3142e4416abd5f6904990e5a9c.png
//取域名
void test_string4()
{
lrq::string s1 = "https://mpbeta.csdn.net/mp_blog/creation/editor/147116669?spm=1000.2115.3001.4503";
size_t pos1 = s1.find(':');
size_t pos2 = s1.find('/', pos1 + 3);
//判断是否找到目标字符
if (pos1 != string::npos && pos2 != string::npos)//npos受类域影响
{
lrq::string domain = s1.substr(pos1 + 3, pos2 - (pos1 + 3));//注意pos1和pos2到底指向哪些个位置
cout << domain.c_str() << endl;
}
} 5.拷贝—拷贝构造和赋值
https://i-blog.csdnimg.cn/direct/7e80e5d06d4148bea7da458ec487ca98.png
我们不表现写拷贝构造和赋值拷贝(都是一种赋值行为),编译器默认都会举行浅拷贝。(1)像上面的代码将s1拷贝赋值给s2,编译器默认的浅拷贝会导致s2和s1指向同一块空间,函数结束的时候会先析构s2再析构s1导致同一块空间析构两次就会出现上面的运行瓦解;(2)还会导致内存走漏,s2之前的空间没有得到释放。(如果一个类需要表现写析构,那么一样平常就需要写拷贝构造和赋值)
https://i-blog.csdnimg.cn/direct/e51143158b72438b8dc42c01235b9963.png
//s1 = s3
string& string::operator=(const string& s)
{
if (this != &s)
{
//上来先将s1给释放掉
delete[] _str;
_str = new char;//预留一个\0的空间
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
return *this;
} 6.流插入
我们输入的是abcd efg,中间包罗一个空格字符,但是ch好像并没有从缓冲区里面提取到空格,看下图,记着,cin在直接使用流提取的时候无论输入任何范例,int,char,它都会默认忽略掉空格和换行,规定以为空格和换行都是多个值之间的分割,也就无法判定什么时候结束,以是我们要举行代码的修改。
https://i-blog.csdnimg.cn/direct/3de022c885f243fe9854743920a468b1.png
我们使用istream类里面的get,可以提取到空格和换行
https://i-blog.csdnimg.cn/direct/bccb9e1f3b334c63829497b6ea270970.png
https://i-blog.csdnimg.cn/direct/bffcf948e94e4d3b85a518c0f8c3f384.png
//cin>>str,这个实现的功能和前面讲的getline很像,只不过结束字符不同而已,我们要融会贯通一下
istream& operator>>(istream& is, string& str)
{
str.clear();//清空str里面原来的字符,否则会继续加在str上面,这是实现和STL里面的string::>>一样的效果
char ch;
ch = is.get();
while (ch != ' ' && ch != '\n')
{
str += ch;
ch = is.get();
}
return is;
} 优化一下:
istream& operator>>(istream& is, string& str)
{
str.clear();
int i = 0;
char buff;
char ch;
ch = is.get();
while (ch != ' ' && ch != '\n')
{
// 放到buff
buff = ch;
if (i == 255)
{
buff = '\0';
str += buff;
i = 0;
}
ch = is.get();
}
if (i > 0)
{
buff = '\0';
str += buff;
}
return is;
} 7.三个swap
https://i-blog.csdnimg.cn/direct/0f75bffbd9554fb78cc0ba18aa98c82a.png
二、
2.1 经典的string类题目
上面已经对string类举行了简单的介绍,各人只要可以或许正常使用即可。在面试中,面试官总喜好让学生自己来模拟实现string类,最重要是实现string类的构造、拷贝构造、赋值运算符重载以及析构函数。各人看下以下string类的实现是否有题目?// 为了和标准库区分,此处使用String
class String
{
public:
/*String()
:_str(new char)
{*_str = '\0';}
*/
//String(const char* str = "\0") 错误示范
//String(const char* str = nullptr) 错误示范
String(const char* str = "")
{
// 构造String类对象时,如果传递nullptr指针,可以认为程序非
if (nullptr == str)
{
assert(false);
return;
}
_str = new char;
strcpy(_str, str);
}
~String()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
};
// 测试
void TestString()
{
String s1("hello bit!!!");
String s2(s1);
} https://i-blog.csdnimg.cn/direct/c5c23ac971b1428ba4b358e6d515edf2.png
说明:上述String类没有显式定义其拷贝构造函数与赋值运算符重载,此时编译器会合成默认的,当用s1构造s2时,编译器会调用默认的拷贝构造。最终导致的题目是,s1、s2共用同一块内存空间,在释放时同一块空间被释放多次而引起步伐瓦解,这种拷贝方式,称为浅拷贝。 2.2 浅拷贝
https://i-blog.csdnimg.cn/direct/326f920fe55d49f997d56e8a0ef79a8c.png
浅拷贝:也称位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,末了就会导致多个对象共享同一份资源,当一个对象烧毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为尚有效,以是当继续对资源进项操作时,就会发生发生了访问违规。就像一个家庭中有两个孩子,但父母只买了一份玩具,两个孩子愿意一块玩,则万事大吉,万一不想分享就你争我夺,玩具损坏。 可以接纳深拷贝解决浅拷贝题目,即:每个对象都有一份独立的资源,不要和其他对象共享。父母给每个孩子都买一份玩具,各自玩各自的就不会有题目了。 2.3 深拷贝
如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一样平常环境都是按照深拷贝方式提供。https://i-blog.csdnimg.cn/direct/94d6c0c57778415ba26bcdd04b0f4895.png
(1)【传统版写法的String类】
class String
{
public:
String(const char* str = "")
{
// 构造String类对象时,如果传递nullptr指针,可以认为程序非
if (nullptr == str)
{
assert(false);
return;
}
_str = new char;
strcpy(_str, str);
}
String(const String& s)
: _str(new char)
{
strcpy(_str, s._str);
}
String& operator=(const String& s)
{
if (this != &s)
{
char* pStr = new char;
strcpy(pStr, s._str);
delete[] _str;
_str = pStr;
}
return *this;
}
~String()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
}; (2)【现代版写法的String类】
https://i-blog.csdnimg.cn/direct/3d5047b189984606b365011a3d2c6e8e.png
class String
{
public:
String(const char* str = "")
{
if (nullptr == str)
{
assert(false);
return;
}
_str = new char;
strcpy(_str, str);
}
String(const String& s)
: _str(nullptr)
{
String strTmp(s._str);
swap(_str, strTmp._str);
}
// 对比下和上面的赋值那个实现比较好?
String& operator=(String s)
{
swap(_str, s._str);
return *this;
}
/*
String& operator=(const String& s)
{
if(this != &s)
{
String strTmp(s);
swap(_str, strTmp._str);
}
return *this;
}
*/
~String()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
}; (3)【写时拷贝】(了解)
https://i-blog.csdnimg.cn/direct/4d201eff7dc74883a35583dfe8cb429e.png
写时拷贝就是一种拖延症,是在浅拷贝的底子之上增加了引用计数的方式来实现的。 引用计数:用来记录资源使用者的个数。在构造时,将资源的计数给成1,每增加一个对象使用该资源,就给计数增加1,当某个对象被烧毁时,先给该计数减1,然后再检查是否需要释放资源,如果计数为1,说明该对象时资源的末了一个使用者,将该资源释放;否则就不能释放,因为尚有其他对象在使用该资源。 写时拷贝 写时拷贝在读取时的缺陷 (4)扩展阅读
面试中string的一种精确写法
STL 的string类怎么啦?
(5)编码
https://i-blog.csdnimg.cn/direct/cb28d0cdcb5148eb8705745155b599f1.png
三、综合代码
string.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace bit
{
class string
{
public:
//typedef char* iterator;
using iterator = char*;
using const_iterator = const char*;
//string();
string(const char* str = "");
string(const string& s);
string& operator=(string s);
~string();
void reserve(size_t n);
void push_back(char ch);
void append(const char* str);
string& operator+=(char ch);
string& operator+=(const char* str);
void insert(size_t pos, char ch);
void insert(size_t pos, const char* str);
void erase(size_t pos, size_t len = npos);
size_t find(char ch, size_t pos = 0);
size_t find(const char* str, size_t pos = 0);
char& operator[](size_t i)
{
assert(i < _size);
return _str;
}
const char& operator[](size_t i) const
{
assert(i < _size);
return _str;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str+_size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
size_t size() const
{
return _size;
}
const char* c_str() const
{
return _str;
}
void clear()
{
_str = '\0';
_size = 0;
}
void swap(string& s);
string substr(size_t pos, size_t len = npos);
private:
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
public:
//
//static const size_t npos = -1;
static const size_t npos;
};
void swap(string& s1, string& s2);
bool operator== (const string& lhs, const string& rhs);
bool operator!= (const string& lhs, const string& rhs);
bool operator> (const string& lhs, const string& rhs);
bool operator< (const string& lhs, const string& rhs);
bool operator>= (const string& lhs, const string& rhs);
bool operator<= (const string& lhs, const string& rhs);
ostream& operator<<(ostream& os, const string& str);
istream& operator>>(istream& is, string& str);
istream& operator>>(istream& is, string& str);
istream& getline(istream& is, string& str, char delim = '\n');
} string.cpp
#include"string.h"namespace bit{ const size_t string::npos = -1; // 11:52 /*string::string() :_str(new char{ '\0' }) , _size(0) , _capacity(0) {}*/ string::string(const char* str) :_size(strlen(str)) { _capacity = _size; _str = new char; strcpy(_str, str); } // 传统写法 // s2(s1) /*string::string(const string& s) { _str = new char; strcpy(_str, s._str); _size = s._size; _capacity = s._capacity; }*/ // s2(s1) // 现代写法 string::string(const string& s) { string tmp(s._str); swap(tmp); } // s2 = s1 = s3 // s1 = s1; // 传统写法 /*string& string::operator=(const string& s) { if (this != &s) { delete[] _str; _str = new char; strcpy(_str, s._str); _size = s._size; _capacity = s._capacity; } return *this; }*/ // 现代写法 // s1 = s3 string& string::operator=(string s) { swap(s); return *this; } string::~string() { delete[] _str; _str = nullptr; _size = 0; _capacity = 0; } void string::reserve(size_t n) { if (n > _capacity) { //cout << "reserve:" << n << endl; char* tmp = new char; strcpy(tmp, _str); delete[] _str; _str = tmp; _capacity = n; } } void string::push_back(char ch) { /*if (_size == _capacity) { reserve(_capacity == 0 ? 4 : _capacity * 2); } _str = ch; _size++;*/ insert(_size, ch); } void string::append(const char* str) { //size_t len = strlen(str); //if (_size + len > _capacity) //{ // size_t newCapacity = 2 * _capacity; // // 扩2倍不够,则需要多少扩多少 // if (newCapacity < _size + len) // newCapacity = _size + len; // reserve(newCapacity); //} //strcpy(_str + _size, str); //_size += len; insert(_size, str); } string& string::operator+=(char ch) { push_back(ch); return *this; } string& string::operator+=(const char* str) { append(str); return *this; } void string::insert(size_t pos, char ch) { assert(pos <= _size); if (_size == _capacity) { reserve(_capacity == 0 ? 4 : _capacity * 2); } /*int end = _size; while (end >= (int)pos) { _str = _str; --end; }*/ size_t end = _size + 1; while (end > pos) { _str = _str; --end; } _str = ch; _size++; } void string::insert(size_t pos, const char* str) { assert(pos <= _size); size_t len = strlen(str); if (_size + len > _capacity) { size_t newCapacity = 2 * _capacity; // 扩2倍不够,则需要多少扩多少 if (newCapacity < _size + len) newCapacity = _size + len; reserve(newCapacity); } /*int end = _size; while (end >= (int)pos) { _str = _str; --end; }*/ size_t end = _size + len; while (end > pos + len - 1) { _str = _str; --end; } for (size_t i = 0; i < len; i++) { _str = str; } _size += len; } void string::erase(size_t pos, size_t len) { assert(pos < _size); if (len >= _size - pos) { _str = '\0'; _size = pos; } else { // 从后往前挪 size_t end = pos + len; while (end <= _size) { _str = _str; ++end; } _size -= len; } } size_t string::find(char ch, size_t pos) { assert(pos < _size); for (size_t i = pos; i < _size; i++) { if (ch == _str) return i; } return npos; } size_t string::find(const char* str, size_t pos) { assert(pos < _size); const char* ptr = strstr(_str + pos, str); if (ptr == nullptr) { return npos; } else { return ptr - _str; } } string string::substr(size_t pos, size_t len) { assert(pos < _size); // 大于后面剩余串的长度,则直接取到结尾 if (len > (_size - pos)) { len = _size - pos; } bit::string sub; sub.reserve(len); for (size_t i = 0; i < len; i++) { sub += _str; } //cout << sub.c_str() << endl; return sub; } void string::swap(string& s) { std::swap(_str, s._str); std::swap(_size, s._size); std::swap(_capacity, s._capacity); } void swap(string& s1, string& s2) { s1.swap(s2); } bool operator== (const string& lhs, const string& rhs) { return strcmp(lhs.c_str(), rhs.c_str()) == 0; } bool operator!= (const string& lhs, const string& rhs) { return !(lhs == rhs); } bool operator> (const string& lhs, const string& rhs) { return !(lhs <= rhs); } bool operator< (const string& lhs, const string& rhs) { return strcmp(lhs.c_str(), rhs.c_str()) < 0; } bool operator>= (const string& lhs, const string& rhs) { return !(lhs < rhs); } bool operator<= (const string& lhs, const string& rhs) { return lhs < rhs || lhs == rhs; } ostream& operator<<(ostream& os, const string& str) { //os<<'"'; //os << "xx\"xx"; for (size_t i = 0; i < str.size(); i++) { //os << str; os << str; } //os << '"'; return os; } istream& operator>>(istream& is, string& str)
{
str.clear();
int i = 0;
char buff;
char ch;
ch = is.get();
while (ch != ' ' && ch != '\n')
{
// 放到buff
buff = ch;
if (i == 255)
{
buff = '\0';
str += buff;
i = 0;
}
ch = is.get();
}
if (i > 0)
{
buff = '\0';
str += buff;
}
return is;
} istream& getline(istream& is, string& str, char delim) { str.clear(); int i = 0; char buff; char ch; ch = is.get(); while (ch != delim) { // 放到buff buff = ch; if (i == 255) { buff = '\0'; str += buff; i = 0; } ch = is.get(); } if (i > 0) { buff = '\0'; str += buff; } return is; }} test.cpp
#include"string.h"
void test_string1()
{
bit::string s2;
cout << s2.c_str() << endl;
bit::string s1("hello world");
cout << s1.c_str() << endl;
s1 = 'x';
cout << s1.c_str() << endl;
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1 << " ";
}
cout << endl;
// 迭代器 -- 像指针一样的对象
bit::string::iterator it1 = s1.begin();
while (it1 != s1.end())
{
(*it1)--;
++it1;
}
cout << endl;
it1 = s1.begin();
while (it1 != s1.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
// 修改
// 底层是迭代器的支持
// 意味着支持迭代器就支持范围for
for (auto& ch : s1)
{
ch++;
}
for (auto ch : s1)
{
cout << ch << " ";
}
cout << endl;
const bit::string s3("xxxxxxxxx");
for (auto& ch : s3)
{
//ch++;
cout << ch << " ";
}
cout << endl;
}
void test_string2()
{
bit::string s1("hello world");
cout << s1.c_str() << endl;
s1 += '#';
s1 += "#hello world";
cout << s1.c_str() << endl;
bit::string s2("hello world");
cout << s2.c_str() << endl;
s2.insert(6, 'x');
cout << s2.c_str() << endl;
s2.insert(0, 'x');
cout << s2.c_str() << endl;
bit::string s3("hello world");
cout << s3.c_str() << endl;
s3.insert(6, "xxx");
cout << s3.c_str() << endl;
s3.insert(0, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
cout << s3.c_str() << endl;
}
void test_string3()
{
bit::string s1("hello world");
cout << s1.c_str() << endl;
s1.erase(6, 2);
cout << s1.c_str() << endl;
s1.erase(5, 20);
cout << s1.c_str() << endl;
s1.erase(3);
cout << s1.c_str() << endl;
}
void test_string4()
{
bit::string s1("hello world");
cout << s1.find(' ') << endl;
cout << s1.find("wo") << endl;
bit::string s2 = "https://legacy.cplusplus.com/reference/cstring/strstr/?kw=strstr";
//bit::string s2 = "https://blog.csdn.net/ww753951/article/details/130427526";
size_t pos1 = s2.find(':');
size_t pos2 = s2.find('/', pos1+3);
if (pos1 != string::npos && pos2 != string::npos)
{
bit::string domain = s2.substr(pos1 + 3, pos2 - (pos1 + 3));
cout << domain.c_str() << endl;
bit::string uri = s2.substr(pos2+1);
cout << uri.c_str() << endl;
}
}
void test_string5()
{
bit::string s1("hello world");
bit::string s2(s1);
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s1 = 'x';
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
bit::string s3("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
s1 = s3;
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
s1 = s1;
cout << s1.c_str() << endl;
}
void test_string6()
{
//bit::string s1("hello world");
//bit::string s2(s1);
//bit::string s3 = s1;
构造+拷贝 ->优化直接构造
//bit::string s4 = "hello world";
//cout << (s1 == s2) << endl;
//cout << (s1 < s2) << endl;
//cout << (s1 > s2) << endl;
//cout << (s1 == "hello world") << endl;
//cout << ("hello world" == s1) << endl;
operator<<(cout, s1);
//cout << s1 << endl;
//cin >> s1;
//cout << s1 << endl;
//
//std::string ss1("hello world");
//cin >> ss1;
//cout << ss1 << endl;
bit::string s1;
//cin >> s1;
//cout << s1 << endl;
getline(cin, s1);
cout << s1 << endl;
getline(cin, s1, '#');
cout << s1 << endl;
}
//void test_string7()
//{
// bit::string s1("hello world");
// bit::string s2("xxxxxxxxxxxxxxxxxxxxxxx");
//
// //swap(s1, s2);
// s1.swap(s2);
// cout << s1 << endl;
// cout << s2 << endl;
//}
void test_string8()
{
bit::string s1("hello world");
bit::string s2(s1);
bit::string s3("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
s1 = s3;
cout << s1 << endl;
cout << s3 << endl;
char arr1[] = "types";
char16_t arr2[] = u"types";
char32_t arr3[] = U"types";
wchar_t arr4[] = L"types";
cout << sizeof(arr1) << endl;
cout << sizeof(arr2) << endl;
cout << sizeof(arr3) << endl;
cout << sizeof(arr4) << endl;
char arr5[] = "苹果 apple";
cout << sizeof(arr5) << endl;
arr5++;
arr5++;
arr5--;
arr5--;
arr5--;
arr5--;
arr5--;
arr5--;
arr5--;
}
//int main()
//{
// test_string8();
//
// return 0;
//}
#include<vector>
int main()
{
vector<int> v1;
vector<int> v2(10, 1);
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
// 遍历
for (size_t i = 0; i < v1.size(); i++)
{
cout << v1 << " ";
}
cout << endl;
vector<int>::iterator it1 = v1.begin();
while (it1 != v1.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
vector<string> vstr;
string s1 = "张三";
vstr.push_back(s1);
vstr.push_back("李四");
for (const auto& e : vstr)
{
cout << e << " ";
}
cout << endl;
vstr += 'x';
vstr += "apple";
//vstr++;
vstr++;
vstr++;
vstr.operator[](0).operator[](1)++;
for (const auto& e : vstr)
{
cout << e << " ";
}
cout << endl;
return 0;
}
完——
下次继续吧。。。vector
我和你—神奇阿呦
https://i-blog.csdnimg.cn/direct/0df5077da993452e8cf542df5a6c235f.jpeg
至此结束!
我是云边有个稻草人
期待与你的下一次相遇!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]