【问题标题】:Acess and modify a C++ vector containing objects with different variable types访问和修改包含具有不同变量类型的对象的 C++ 向量
【发布时间】:2020-12-12 21:03:24
【问题描述】:

我刚开始学习 C++,我想访问和修改具有不同变量类型的对象向量。我只能创建矢量,但不能用它做任何事情。我怎样才能访问和修改它? 这是我到目前为止创建的代码:

class Person {
private: string name; int id;
public: 
    Person(string x, int y) { name = x; id = y; };
    ~Person(); 
};

int main()
{

vector<vector<Person>> v4;

}

【问题讨论】:

  • 一个向量不能包含不同的变量类型。
  • 所以我必须为每个变量创建一个向量或者创建结构? - @n.'pronouns'm.

标签: c++ vector


【解决方案1】:

要访问存储在向量中的对象,您有不同的运算符和方法,例如 at()operator[] 更多详细信息请查看https://www.cplusplus.com/reference/vector/vector/

既然你想学习 C++,我假设你不知道具体要问什么,所以让我来帮助你

除了访问向量中的对象之外,您的代码示例还缺少其他内容:

  • 由于nameid 是私有的,您无法访问它们。
  • 你的析构函数没有实现
  • 您创建了 Person 向量的向量。我假设您只需要一个向量。
  • 您关于一个向量中的多种类型的问题:这并不容易。根据您的需要,您需要多个向量,或者需要组合您的数据(例如通过std::pair,或者您需要使用接口和继承。
  • 你的向量是空的

让我构造一些东西来提示你从哪里继续:

#include <iostream>
#include <string>
#include <vector>

class Person {
private: 
    std::string name;
    int id;
public: 
    Person(std::string x, int y) { name = x; id = y; };
    // ~Person();  //This is not needed, since your class currently does not need any special handling during destruction

    // You need to be able to access name and id to be able to do something with them, for example print it
    std::string getName() const { 
        // be aware that this creates a copy of name. Thus changes to the returned string will not be reflected in the Person object.
        // Another option would be returning a reference (std::string&), but that I leave for you to research yourself
        return name;
    } 
    int getId() const {
        // same as for the name applies
        return id;
    }
    // This allows us to change the id of a Person
    void setId(int newId) {
        id = newId;
    } 
};

int main()
{
    // Lets create a Person first:
    Person p ("John", 1); // Please note that the string literal here will be used to construct an std::string on the fly and then store it in name of p

    // Create a second Person:
    Person p2 ("Jim", 2);

    // Create a vector and add the persons to it
    std::vector<Person> v;
    v.push_back(p);
    v.push_back(p2);

    // Print out the contents of the vector
    for (auto& pers : v) {
        std::cout << "Person " << pers.getName() << " has id " << pers.getId() << std::endl;
    }

    // Now change the id of the second person in the vector to 5 (note that indexes start at 0)
    v.at(1).setId(5);

    // Print again
    for (auto& pers : v) {
        std::cout << "Person " << pers.getName() << " has id " << pers.getId() << std::endl;
    }
}

玩弄它:https://godbolt.org/z/8dfEoa

我希望这可以帮助您入门。

【讨论】:

  • TBH,我知道 set/get 函数(但不是析构函数实现)以及单个向量可以包含不同类型的事实。只是我无法访问/修改它们,所以我提出了向量潜在解决方案的向量。但你是对的,这真的让我走上了正确的道路。谢谢。
  • 顺便说一句。不要混淆对象和类型。对象是特定类型的实例化,例如Person 是该类型,而 pp1 是该类型的对象。一个向量可以包含许多对象,但所有对象都必须具有相同的类型。
  • 顺便说一句,类库的向量可以保存派生类库的对象吗?
  • 是的,这是可能的。如果你有class Baseclass Derived : public Base,你可以创建一个vector&lt;Base&gt; 并添加BaseDerived 类型的对象到它。请注意,如果您检索对象,它们的类型将为Base,因此您只有Base 的接口可用。如果您想使用仅在 Derived 中可用的内容,您可以使用 dynamic_cast&lt;Derived*&gt;(...) 了解详情,请参阅 en.cppreference.com/w/cpp/language/dynamic_cast
猜你喜欢
  • 1970-01-01
  • 2012-07-05
  • 2018-05-05
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 2019-04-13
相关资源
最近更新 更多