第一范文网 - 专业文章范例文档资料分享平台

7 第6章 数组、指针与字符串(二)

来源:用户分享 时间:2025/5/30 3:51:48 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

//insert初始化方式将同类型的迭代器对应的始末区间(左闭右开区间)内的值插入到 vector中 vector a(6,6); vecot b;

//将a[0]~a[2]插入到b中,b.size()由0变为3 b.insert(b.begin(), a.begin(), a.begin() + 3); insert也可通过数组地址区间实现插入 int a[6] = {6,6,6,6,6,6}; vector b;

//将a的所有元素插入到b中,同样是左闭右开区间 b.insert(b.begin(), a, a+6);

此外,insert还可以插入m个值为n的元素 //在b开始位置处插入6个6 b.insert(b.begin(), 6, 6); (6)通过copy函数赋值 vector a(5,1); int a1[5] = {2,2,2,2,2}; vector b(10);

/*将a中元素全部拷贝到b开始的位置中,注意拷贝的区间为a.begin() ~ a.end()的左 闭右开的区间*/

copy(a.begin(), a.end(), b.begin()); //拷贝区间也可以是数组地址构成的区间

copy(a1, a1+5, b.begin() + 5); 6.5 深复制与浅复制 6.5.1 深层复制与浅层复制 浅层复制与深层复制

● 浅层复制

■ 实现对象间数据元素的一一对应复制。 ● 深层复制

■ 当被复制的对象数据成员是指针类型时,不是复制该指针成员本身,而是将指针所指

对象进行复制。 例6-21 对象的浅层复制

//例6_21 对象的浅层复制.cpp(P226)

#include #include using namespace std;

class Point { public:

Point() : x(0), y(0) {

cout<<\<

Point(int x, int y) : x(x), y(y) {

cout<< \<

~Point() { cout<<\<

private:

int x, y; };

//动态数组类

class ArrayOfPoints { public:

ArrayOfPoints(int size) : size(size) { points = new Point[size]; }

~ArrayOfPoints() {

cout << \ << endl; delete[] points; }

//获得下标为index的数组元素 Point &element(int index) {

assert(index >= 0 && index < size);//如果数组下标不会越界,程序中止

return points[index]; }

private:

Point *points;//指向动态数组首地址 int size;//数组大小

};

int main() { int count;

cout << \; cin >> count;

ArrayOfPoints pointsArray1(count);//创建对象数组 pointsArray1.element(0).move(5,10); pointsArray1.element(1).move(15,20);

ArrayOfPoints pointsArray2 = pointsArray1;//创建对象数组副本

cout << \ << endl;

cout << \ << pointsArray2.element(0).getX() << \ << pointsArray2.element(0).getY() << endl;

cout << \ << pointsArray2.element(1).getX() << \ << pointsArray2.element(1).getY() << endl;

pointsArray1.element(0).move(25, 30); pointsArray1.element(1).move(35, 40);

cout << \ << endl;

cout << \ << pointsArray2.element(0).getX() << \

}

<< pointsArray2.element(0).getY() << endl;

cout << \ << pointsArray2.element(1).getX() << \ << pointsArray2.element(1).getY() << endl; return 0;

运行结果如下:

Please enter the number of points:2 Default Constructor called. Default Constructor called. Copy of pointsArray1: Point_0 of array2: 5, 10 Point_1 of array2: 15, 20

After the moving of pointsArray1: Point_0 of array2: 25, 30 Point_1 of array2: 35, 40 Deleting...

Destructor called. Destructor called. Deleting...

接下来程序出现运行错误。 例6-22 对象的深层复制

//例6_22 对象的深层复制.cpp(P228)

#include #include using namespace std;

class Point { public:

Point() : x(0), y(0) {

cout<<\<

Point(int x, int y) : x(x), y(y) { cout<< \<

~Point() { cout<<\<

private:

int x, y; };

//动态数组类

class ArrayOfPoints { public:

ArrayOfPoints(int size) : size(size) { points = new Point[size]; }

ArrayOfPoints(const ArrayOfPoints& v); ~ArrayOfPoints() {

cout << \ << endl; delete[] points; }

//获得下标为index的数组元素 Point &element(int index) {

assert(index >= 0 && index < size);//如果数组下标不会越界,程序中止

return points[index]; }

private:

Point *points;//指向动态数组首地址 int size;//数组大小

};

ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints& v) { size = v.size;

points = new Point[size];

for (int i = 0; i < size; i++) points[i] = v.points[i]; }

int main() { int count;

cout << \; cin >> count;

ArrayOfPoints pointsArray1(count);//创建对象数组 pointsArray1.element(0).move(5,10); pointsArray1.element(1).move(15,20);

搜索更多关于: 7 第6章 数组、指针与字符串(二) 的文档
7 第6章 数组、指针与字符串(二).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8ulss5up8i6ksx797jw59jajr88l5800wu5_7.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top