push_back讲解
push_back函数在C++中的vector头文件中,在vector类中作用为在vector尾部加入一个数据。string中也有这个函数,作用是字符串之后插入一个字符。和insert相比push_back,insert放到指定位置,push_back放到最后一位。push_back是stl里面容器的方法。比如vector que;que.push_back(‘a’);que.pus
push_back函数在C++中的vector头文件中,在vector类中作用为在vector尾部加入一个数据。string中也有这个函数,作用是字符串之后插入一个字符。和insert相比push_back,insert 放到指定位置,push_back放到最后一位。
push_back是stl里面容器的方法。
比如vector que;
que.push_back(‘a’);
que.push_back(‘b’);
push_back在作用是在容器里添加相应的元素。
que.push_back((char)(dep+1+’0’));的意思是向容器中加入一个(dep+1+’0’)的字符
例如:
当主函数只有一个push_back时:
int main()
{
vector sb;
HP a;
sb.push_back(a);
while(1);
}
由于原来的vector为空,所以只是分配新的空间,然后拷贝push_back的元素。所以结果如下:
gouzao //这是 语句 HP a; 构造a产生
kaobei //这是 语句 sb.push_back(a) 拷贝新元素产生的。
当主函数有两个push_back时:
int main()
{
vector sb;
HP a;
sb.push_back(a);
sb.push_back(a);
while(1);
}
注意执行第二个push_back时,首先另外分配两个元素的空间,并将原来的vector元素拷过来,后拷贝新push的元素,最后析构原来的元素。故结果如下 :
gouzao //这是 语句 HP a; 构造a产生
kaobei //这是 第一个sb.push_back(a) 拷贝新元素产生的。
kaobei //下面这两个是由第二个sb.push_back(a)产生的,它先先将原来的vector元素拷过来,后拷贝新push的元素
kaobei
xigou //只是析构原来的vector元素产生的。
当主函数有三个push_back时:一样分析,
int main()
{
vector sb;
HP a;//构造
sb.push_back(a); //一个拷贝
sb.push_back(a);//两个拷贝,一个析构
sb.push_back(a);//三个拷贝,两个析构
while(1);
}
结果如下:
gouzao
kaobei
kaobei
kaobei
xigou
kaobei
kaobei
kaobei
xigou
xigou
下面是看别人博客看到的例子:
vector有预存的内存(capacity),如果存入的元素大于了capacity,就重新分配一个比原来capacity大两倍的内存。
#include <iostream>
#include <vector>
using namespace std;
class test {
public:
string a;
test(string b) : a(b) {
cout << "new" << " " << a << endl;
}
~test() {
cout << "deleted " << a << endl;
}
};
int main(int argc, const char * argv[]) {
vector<test> array;
string a = "A";
array.push_back(test("a"));
cout << array.capacity() << endl;
array.push_back(test("b"));
cout << array.capacity() << endl;
array.push_back(test("c"));
cout << array.capacity() << endl;
array.push_back(test("d"));
cout << array.capacity() << endl;
array.push_back(test("f"));
cout << array.capacity() << endl;
return 0;
}
输出:
new a
deleted a
1
new b
deleted a
deleted b
2
new c
deleted b
deleted a
deleted c
4
new d
deleted d
4
new f
deleted d
deleted c
deleted b
deleted a
deleted f
8
deleted f
deleted d
deleted c
deleted b
deleted a
Program ended with exit code: 0
结合一起看:
#include <iostream>
#include <vector>
using namespace std;
struct test{
test(char c) :a(c)
{
cout << "New " << a << endl;
}
~test()
{
cout << "delete " << a << endl;
}
char a;
};
int main()
{
vector<test> t;
for (char c = 'a'; c < 'z' + 1; ++c) {
t.push_back(c);
cout << t.capacity() << endl;
}
system("pause");
}
输出:
New a
delete a
1
New b
delete a
delete b
2
New c
delete b
delete a
delete c
4
New d
delete d
4
New e
delete d
delete c
delete b
delete a
delete e
8
New f
delete f
8
New g
delete g
8
New h
delete h
8
New i
delete h
delete g
delete f
delete e
delete d
delete c
delete b
delete a
delete i
16
New j
delete j
16
New k
delete k
16
New l
delete l
16
New m
delete m
16
New n
delete n
16
New o
delete o
16
New p
delete p
16
New q
delete p
delete o
delete n
delete m
delete l
delete k
delete j
delete i
delete h
delete g
delete f
delete e
delete d
delete c
delete b
delete a
delete q
32
New r
delete r
32
New s
delete s
32
New t
delete t
32
New u
delete u
32
New v
delete v
32
New w
delete w
32
New x
delete x
32
New y
delete y
32
New z
delete z
32
delete z
delete y
delete x
delete w
delete v
delete u
delete t
delete s
delete r
delete q
delete p
delete o
delete n
delete m
delete l
delete k
delete j
delete i
delete h
delete g
delete f
delete e
delete d
delete c
delete b
delete a
Program ended with exit code: 0
更多推荐
所有评论(0)