栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习笔记(Day10 vector对象)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习笔记(Day10 vector对象)

vector 对象
  • 封装任何类型的动态数组,自动创建和删除。
  • 数组下标越界检查。
  • 例6-18中封装的ArrayOfPoints也提供了类似功能,但只适用于一种类型的数组。
vector 对象的定义
  • vector 数组对象名(数组长度);
  • 例: vector arr(5) 建立大小为5的int数组
vector 对象的使用
  • 对数组元素的引用
  • 与普通数组具有相同形式: vector对象名 [ 下标表达式 ]
  • vector数组对象名不表示数组首地址
  • 获得数组长度
  • 用size函数 数组对象名.size()

例 6-20 vector 应用举例

#include 
#include 
using namespace std;

double average(const vector &arr){
    double sum = 0;
    for(unsigned i = 0; i < arr.size();i++)
        sum += arr[i];
    return sum/arr.size();
}
int main(){
    unsigned n;
    cout << "n =  ";
    cin >> n;
    vector arr(n);//创建数组对象
    cout << "Please input " << n << " real numbersL " << endl;
    for(unsigned i = 0;i> arr[i];
    cout << "The average =" << average(arr) << endl;
    return 0;
}

 基于范围的for循环配合auto举例

#include 
#include 
using namespace std;


int main(){
    vector v ={1,2,3};
    for(auto i = v.begin();i != v.end();++i)
        cout << *i << endl;
    for(auto e:v)
        cout << e << endl;
    return 0;
}
对象的复制与移动

浅层复制

  •     实现对象间数据元素的一一对应复制。

深层复制

  •     当被复制的对象数据成员是指针类型时,不是复制该指针成员本身,而是将指针所指对象进行复制。
//例 6-21 对象的浅层复制
#include 
#include 
using namespace std;
class Point {
public:
    Point() : x(0), y(0) {
        cout<<"Default Constructor called."<= 0 && index < size);
        return points[index];
    }
private:
    Point *points; //指向动态数组首地址
    int size; //数组大小
};
int main() {
    int count;
    cout << "Please enter the count of points: ";
    cin >> count;
    ArrayOfPoints pointsArray1(count); //创建对象数组
    pointsArray1.element(0).move(5,10);
    pointsArray1.element(1).move(15,20);

    ArrayOfPoints pointsArray2(pointsArray1); //创建副本

    cout << "Copy of pointsArray1:" << endl;
    cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
         << pointsArray2.element(0).getY() << endl;
    cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
         << pointsArray2.element(1).getY() << endl;
    pointsArray1.element(0).move(25, 30);
    pointsArray1.element(1).move(35, 40);

    cout<<"After the moving of pointsArray1:"< 
例 6-22 对象的深层复制 
//例 6-21 对象的浅层复制
#include 
#include 
using namespace std;
class Point {
public:
    Point() : x(0), y(0) {
        cout<<"Default Constructor called."<= 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 << "Please enter the count of points: ";
    cin >> count;
    ArrayOfPoints pointsArray1(count); //创建对象数组
    pointsArray1.element(0).move(5,10);
    pointsArray1.element(1).move(15,20);

    ArrayOfPoints pointsArray2(pointsArray1); //创建副本

    cout << "Copy of pointsArray1:" << endl;
    cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
         << pointsArray2.element(0).getY() << endl;
    cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
         << pointsArray2.element(1).getY() << endl;
    pointsArray1.element(0).move(25, 30);
    pointsArray1.element(1).move(35, 40);

    cout<<"After the moving of pointsArray1:"< 
 

移动构造

class_name ( class_name && )

&&:右值引用

例:函数返回含有指针成员的对象(版本 1)

  • 使用深层复制构造函数
  • 返回时构造临时对象,动态分配将临时对象返回到主调函数,然后删除临时对象。
#include 
using namespace std; 
class IntNum { 
public: 
 IntNum(int x = 0) : xptr(new int(x)){ //构造函数 
 cout << "Calling constructor..." << endl; 
 } 
 IntNum(const IntNum & n) : xptr(new int(*n.xptr)){//复制构造函数 
 cout << "Calling copy constructor..." << endl; 
 }; 
 ~IntNum(){ //析构函数 
 delete xptr; 
 cout << "Destructing..." << endl; 
 } 
 int getInt() { return *xptr; } 
private: 
 int *xptr; 
}; 
//返回值为IntNum类对象 
IntNum getNum() { 
 IntNum a; 
 return a; 
} 
int main() { 
 cout< 

例:函数返回含有指针成员的对象(版本 2)

  • 使用移动构造函数
  • 将要返回的局部对象转移到主调函数,省去了构造和删除临时对象的过程。

 

#include 
using namespace std; 
class IntNum { 
public: 
 IntNum(int x = 0) : xptr(new int(x)){ //构造函数 
 cout << "Calling constructor..." << endl; 
 } 
 IntNum(const IntNum & n) : xptr(new int(*n.xptr)){//复制构造函数 
 cout << "Calling copy constructor..." << endl; 
 } 
IntNum(IntNum && n): xptr( n.xptr){ //移动构造函数 注:&&是右值引用 函数返回的临时变量是右值
 n.xptr = nullptr; 
 cout << "Calling move constructor..." << endl; 
 } 
 ~IntNum(){ //析构函数 
 delete xptr; 

 cout << "Destructing..." << endl; 
 } 
private: 
 int *xptr; 
}; 
//返回值为IntNum类对象 
IntNum getNum() { 
IntNum a; 
return a; 
} 
int main() { 
cout << getNum().getInt() << endl; return 0; 
} 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316965.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号