首页 > 编程语言 >C++中 vector容器的神奇用法

C++中 vector容器的神奇用法

时间:2022-11-13 17:26:12浏览次数:40  
标签:Boy const string int C++ 用法 vector Girl include

1.可以用简单的数据类型作为参数:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> data;
    data.push_back(1);
    data.push_back(5);
    for (int i = 0; i < data.size(); i++) {
        cout << data[i] << " ";
    }
    system("pause");
    return 0;
}

 2.也可以用一个类作为参数:

Boy.h:

#pragma once
#include <string>
using namespace std;
class Girl;
class Boy
{
public:
    Boy();
    Boy(int age,string name,int salary);
    int getAge()const;
    string getName()const;
    int getSalary()const;
    bool isSatisfied(const Girl& girl)const;
    string description()const;
    ~Boy();
private:
    int age;
    string name;
    int salary;

};

Girl.h:

#pragma once
#include <iostream>
using namespace std;
class Boy;
class Girl
{
public:
    Girl();
    Girl(string name,int age,int faceScore);
    int getAge()const;
    string getName()const;
    int getFaceScore()const;
    bool isSatisfied(const Boy& boy)const;
    string description()const;
    ~Girl();
private:
    int age;
    string name;
    int faceScore; //颜值
};

Boy.cpp:

#include <sstream>
#include "Boy.h"
#include "Girl.h"
#define SALARY_FACTORY 0.01
Boy::Boy()
{
    age = 0;
    name = "";
    salary = 0;
}
Boy::Boy(int age, string name, int salary) {
    this->age = age;
    this->name = name;
    this->salary = salary;
}
int Boy::getAge()const {
    return age;
}
string Boy::getName() const {
    return name;
}
int Boy::getSalary()const {
    return salary;
}
bool Boy::isSatisfied(const Girl& girl)const {
    if (girl.getFaceScore() >= salary * SALARY_FACTORY) {
        return true;
    }
    else
    {
        return false;
    }
}
string Boy::description()const {
    stringstream ret;
    ret << name << "-男-薪资(" << salary << ")-年龄(" << age << ")";
    return ret.str();
}
Boy::~Boy() {

}

Girl.cpp:

#include "Girl.h"
#include "Boy.h"
#include <sstream>
#define FACESCORE_FACTORY 1

Girl::Girl() {
    name = "";
    age = 0;
    faceScore = 0;
}
Girl::Girl(string name, int age, int faceScore) {
    this->name = name;
    this->age = age;
    this->faceScore = faceScore;
}
int Girl::getAge()const {
    return age;
}
string Girl::getName()const {
    return name;
}
int Girl::getFaceScore()const {
    return faceScore;
}
bool Girl::isSatisfied(const Boy& boy)const {
    if (boy.getSalary() >= faceScore * FACESCORE_FACTORY) {
        return true;
    }
    else
    {
        return false;
    }
}
string Girl::description()const {
    stringstream ret;
    ret << name << "-女-颜值(" << faceScore << ")-年龄(" << age << ")";
    return ret.str();
}
Girl::~Girl() {
    
}

主函数main.cpp:

#include <iostream>
#include <vector>
#include "Girl.h"
#include "Boy.h"

using namespace std;

int main() {
    Boy boy1(26,"小刘",25000);
    Boy boy2(24,"小王",15000);
    Girl girl1("小花",25,13000);
    Girl girl2("小美", 23, 10000);
    vector<Boy> boys;
    vector<Girl> girls;
    boys.push_back(boy1);
    boys.push_back(boy2);
    girls.push_back(girl1);
    girls.push_back(girl2);
    for (int i = 0; i < boys.size(); i++) {
        cout << boys[i].description() << endl;
    }
    for (int j = 0; j < girls.size(); j++) {
        cout << girls[j].description() << endl;
    }
    system("pause");
    return 0;
}

最后的实现结果:

标签:Boy,const,string,int,C++,用法,vector,Girl,include
From: https://www.cnblogs.com/smartlearn/p/16886334.html

相关文章

  • C++设计模式-(创建模式)原型模式
     原型模式主要用于复制当前对象的副本 #include<iostream>classanimal{public:virtual~animal(){}virtualvoideat(){std::cout......
  • 按照C++语言程序结构组成数字电路进行计算的计算机
    按照C++语言程序结构组成数字电路进行计算的计算机按照C++语言程序结构组成数字电路进行计算的计算机是一种可以按照C++语言程序结构来安排加法器,输出显示电路,输入电路,的数......
  • Size of的用法
    1#include<cstdlib>2#include<iostream>3#include<iterator>4#include"string.h"5usingnamespacestd;6struct{7shorta1;8shorta2......
  • Springboot中-全局异常处理类用法示例
    使用springboot搭建web项目的时候,一般都会添加一个全局异常类,用来统一处理各种自定义异常信息,和其他非自定义的异常信息,以便于统一返回错误信息。下面就是简单的示例......
  • 类和对象——C++运算符重载
    概念:对已有的运算符重新定义,赋予其另一种功能,以适应不同数据类型。。重载的两类方法:1.类成员函数重载2.全局函数重载注意:运算符重载也可以实现函数重载引入:我们希望......
  • C++ template using for what?
    //Forclassusing,youcansetaseriousofmacrotopredictsomething////whenyoucallthismacro,thetemplatecouldmakeanewfunctionbyyourinput//templ......
  • C++运算符重载相关知识点
    1.运算符重载限制重载后的运算符必须至少有一个操作数是用户自定义的类型使用运算符时不能违反运算符原来的句法规则,也不能修改运算符的优先级。2.不能进行重载的运......
  • Nginx 常规用法
    Nginx简介Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамбле......
  • C++之string的底层简单实现!(七千字长文详解)
    C++之string的底层简单实现!string之私有成员变量namespaceMySTL{classstring {private: char*_str; size_t_size; size_t_capacity; //这里capa......
  • 周日1040C++班级2022-11-13 数据类型-字符型char
    数据类型-char字符型特点:由单引号’’构成,且长度为1,在格式化中字符用%c来表示正确的字符:‘a’ ‘ ’ ‘#’ ‘1’错误的字符:’aa’ ‘##’ ‘’’’ascii码表......