电商专业学习嵌入式软件开发第六十一天

  • C++第七天

今天主要讲vector类模板和迭代器,以及软件qt的大概使用方法。今天代码的意思不难理解,就是自己写不出来,尤其是今天留的两道作业,第一题是刚讲过,但是整理不出头绪。

#include <iostream>
#include <vector>
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组
void print(vector<int> &vec)
{
    //迭代器,类似于指针
    vector<int>::iterator iter;
    iter = vec.begin();
    for (; iter != vec.end(); iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
}
int main()
{
    vector<int> vec;
    //vector最开始空间为0,不能直接通过下标进行访问
    //vec[100] = 90;  // X
    //push_back:会自动申请空间,与string分配空间的策略一样
    vec.push_back(89);
    cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
    
    vec.push_back(99);
    cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
    
    vec.push_back(79);
    cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
    
    vec.push_back(69);
    cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
    
    vec.push_back(89);
    cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
    
    vec.push_back(109);
    cout << "size:" << vec.size() <<" capacity:"<<vec.capacity()<<'\n';
    print(vec);
#if 0
    for (int i=0; i < vec.size(); i++)
    {
        cout << vec[i] << ' ';
    }
    cout << endl;
#endif
    cout << "Hello World!" << endl;
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;

class Student{};
typedef vector<Student *> STUVEC;
int main(void)
{
    STUVEC vec;
    vec.push_back(new Student);
    vec.push_back(new Student);
    vec.push_back(new Student);

    STUVEC::iterator iter = vec.begin();

    return 0;
}
#include <iostream>
#include <vector>
using namespace std;

class Student
{
public:
    Student(string name="", float score=0)
    {
        cout << "Student(string,float)\n";
    }
    Student(const Student &other)
    {
        cout << "Student(const Student &)\n";
    }
    ~Student(){cout << "~Student()\n";}
private:
    string m_strName;
    float m_fScore;
};
int main()
{
    vector<Student> vec;
    Student aa("aa", 11);
    vec.push_back(aa);
    
    Student bb("bb", 22);
    vec.push_back(bb);

    Student cc("cc", 33);
    vec.push_back(cc);

    Student dd("dd", 44);
    vec.push_back(dd);

    Student ee("ee", 55);
    vec.push_back(ee);

    Student ff("ff", 66);
    vec.push_back(ff);

    Student gg("gg", 77);
    vec.push_back(gg);

    cout << "Hello World!" << endl;
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;

class Student
{
public:
    Student(string name="")
    {
        m_strName = name;
    }
    const string &getName(){return m_strName;}
private:
    string m_strName;
};
int main(void)
{
    //vector<Student *> vec;
    //vector<Student *> vec[1024];
    string strName;
    vector<vector<Student *> > vec;
    for (int i = 0; i < 3; i++)
    {
        vector<Student *> tmp;
        for (int j = 0; j < 2; j++)
        {
            cout << "input name:";
            cin >> strName;
            tmp.push_back(new Student(strName));
        }
        vec.push_back(tmp);
    }
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << vec[i][j]->getName() << ' ';
        }
        cout << '\n';
    }
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;

class Student
{
public:
    Student(string name="", float score=0)
    {
        cout << "Student(string,float)\n";
    }
    Student(const Student &other)
    {
        cout << "Student(const Student &)\n";
    }
    ~Student(){cout << "~Student()\n";}
private:
    string m_strName;
    float m_fScore;
};
int main()
{
    vector<Student*> vec;
    vec.push_back(new Student("aa", 11));
    vec.push_back(new Student("bb", 22));
    vec.push_back(new Student("cc", 33));
    vec.push_back(new Student("dd", 44));
    vec.push_back(new Student("ee", 55));
    vec.push_back(new Student("ff", 66));
    vec.push_back(new Student("gg", 77));
    vector<Student*>::iterator iter;
    iter = vec.begin();
    for (; iter != vec.end(); iter++)
    {
        delete (*iter);
    }   
    vec.clear();
    cout << "Hello World!" << endl;
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组

void print(vector<int> &vec)
{
    //迭代器,类似于指针
    vector<int>::iterator iter;
    iter = vec.begin();
    for (; iter != vec.end(); iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
}
int main()
{
    //vector<int> vec(32, 888);
    vector<int> vec(32);
    vec.clear();
    cout << vec.size() << ' ' << vec.capacity() << endl;
    print(vec);

    cout << "Hello World!" << endl;
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组

void print(vector<int> &vec)
{
    //迭代器,类似于指针
    vector<int>::iterator iter;
    iter = vec.begin();
    for (; iter != vec.end(); iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
}
int main()
{
    int a[7] = {19, 67, 89, 3, 16, 22, 15};
    vector<int> vec(a, a+7);  //[)
    print(vec);
    //vec.resize(3);
    //vec.resize(13);
    vec.resize(13, 888);
    print(vec);
#if 0
    vec.assign(3, 888);
    print(vec);
#endif
    cout << vec.size() << ' ' << vec.capacity() << endl;

    cout << "Hello World!" << endl;
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组

void print(vector<int> &vec)
{
    //迭代器,类似于指针
    vector<int>::iterator iter;
    iter = vec.begin();
    for (; iter != vec.end(); iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
}
int main()
{
    vector<int> vec;
    vec.reserve(1024);
    cout << "size:" << vec.size() << " capacity:"<<vec.capacity()<<endl;
    print(vec);
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;
//vector:是一个类模板,空间连续,并且空间自动增长,可以把它理解为空间自动增长的数组

void print(vector<int> &vec)
{
    //迭代器,类似于指针
    vector<int>::iterator iter;
    iter = vec.begin();
    for (; iter != vec.end(); iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
}
int main()
{
    int a[7] = {19, 67, 89, 3, 16, 22, 15};
    vector<int> vec(a, a+7);  //[)
    print(vec);
    vector<int>::iterator iter = vec.begin();
    iter = iter+3;
    vec.insert(iter, 888);
    print(vec);
    cout << "----------------\n";
    iter = vec.begin();
    for (;iter != vec.end();)
    {
        if (0 != (*iter)%2)
        {
            //对容器进行增删后,所有迭代器失效
            //必须对迭代器进行重新赋值
            //erase返回值为下一个元素的迭代器
            iter = vec.erase(iter);//对容器进行删除
        }
        else
        {
            iter++;
        }
    }
    print(vec);
    cout << "Hello World!" << endl;
    return 0;
}

作业:
1、a.动态生成多个学生对象,将学生对象的指针放入容器中,然后将容器中的数据写入文件中
b.将文件中的学生数据读取出来放入容器中并显示
2、写一个类模板,用来对不同类型的数据进行排序

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,270评论 18 399
  • 前言: 详细介绍: List:元素有放入顺序,元素可重复Map:元素按键值对存储,无放入顺序Set:元素无放入顺序...
    YBshone阅读 12,803评论 0 17
  • 标签(空格分隔): STL 运用STL,可以充分利用该库的设计,让我为简单而直接的问题设计出简单而直接的解决方案,...
    认真学计算机阅读 5,360评论 0 10
  • 一. Java基础部分.................................................
    wy_sure阅读 9,295评论 0 11
  • 正文开始之前,我必须得申明一直处于中立人群的我跟大多数人一样碌碌无为,每天拼命的忙碌奔波只为了让自己的生活更...
    刘小姐养生杂货铺阅读 3,157评论 0 2

友情链接更多精彩内容