如何在 C++ 中创建类对象的向量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21919277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:52:37  来源:igfitidea点击:

How to create a vector of class objects in C++?

c++vectorreference

提问by Girardi

I am trying to create a simple stack using vector in C++.

我正在尝试使用 C++ 中的向量创建一个简单的堆栈。

Here is the code:

这是代码:

#include <vector>

class Site
{
    public:
        int i; // site position i (x-axis)
        int s; // site state
        vector<Site> neighbors;
        Site(void);
        Site(int ii, int ss);
        void AddNeighbor(Site &site);
};
Site::Site()
{
    i = -1;
    s = -1;
    vector<Site> neighbors;
}
Site::Site(int ii, int ss) 
{
    i = ii;
    s = ss;
}
void Site::AddNeighbor(Site &site)
{
    neighbors.push_back(site);
}

void testStack()
{
    int tot = 600;
    vector<Site> myStack();
    int i = 0;
    while (i < tot)
    {
        Site site(i, 1);
        myStack.push_back(site);
        i++;
    }

    i = 0;
    while (i < tot)
    {
        Site *site = myStack.back();
        myStack.pop_back();
        cout << site->i << site->s << endl;
        i++;
    }
}

Compiler errors:

编译器错误:

ising_wolff.cpp: In function ‘void testStack()': ising_wolff.cpp:373:17: error: request for member ‘push_back' in ‘myStack', which is of non-class type ‘std::vector()' myStack.push_back(site); ^ ising_wolff.cpp:380:30: error: request for member ‘back' in ‘myStack', which is of non-class type ‘std::vector()' Site *site = myStack.back(); ^ ising_wolff.cpp:381:17: error: request for member ‘pop_back' in ‘myStack', which is of non-class type ‘std::vector()' myStack.pop_back();

ising_wolff.cpp:在函数“void testStack()”中:ising_wolff.cpp:373:17:错误:在“myStack”中请求成员“push_back”,它是非类类型“std::vector()”myStack .push_back(网站);^ ising_wolff.cpp:380:30:错误:在“myStack”中请求成员“back”,它是非类类型“std::vector()” Site *site = myStack.back(); ^ ising_wolff.cpp:381:17: 错误:在“myStack”中请求成员“pop_back”,它是非类类型“std::vector()”myStack.pop_back();

What do these errors mean?

这些错误是什么意思?

Here are some sites I have looked at:

以下是我看过的一些网站:

1) Creating objects while adding them into vectors

1)在将对象添加到向量中的同时创建对象

2) push_back causing errors in C

2) push_back 导致 C 中的错误

3) how to create vectors of class object

3)如何创建类对象的向量

回答by Eric Leschinski

How to create a vector of class objects in C++?

如何在 C++ 中创建类对象的向量?

Start with something simpler so you can get the hang of it.

从更简单的事情开始,这样你就可以掌握它的窍门。

First, create a vector of primitive ints:

首先,创建一个原始整数向量:

#include <vector>
#include <iostream>
using namespace std;
int main(){
  vector<int> sites(5);
  sites.push_back(5);
  for(int x = 0; x < sites.size(); x++){
    cout << sites[x];
  }
  cout << endl;
  return 0;
}

Compiling it:

编译它:

g++ -o test test.cpp

Running it:

运行它:

./test
000005

Create a vector of class objects in a similar way as above:

以与上述类似的方式创建类对象的向量:

#include <iostream>
#include <vector>
using namespace std;

class Site {
public:
    int i;
};

int main() {
    vector<Site> listofsites;
    Site *s1 = new Site;
    s1->i = 7;
    Site *s2 = new Site;
    s2->i = 9;
    listofsites.push_back(*s1);
    listofsites.push_back(*s2);
    vector<Site>::iterator it;
    for (it = listofsites.begin(); it != listofsites.end(); ++it) {
        cout << it->i;
    }
    return 0;
}

Which should print:

哪个应该打印:

79

回答by Joseph Mansfield

vector<Site> myStack();

This is actually a function declaration. The function is called myStackand it returns a vector<Site>. What you actually want is:

这实际上是一个函数声明。该函数被调用myStack并返回一个vector<Site>. 你真正想要的是:

vector<Site> myStack;

The type of neighboursat the moment will store copies of the objects, not references. If you really want to store references, I recommend using a std::reference_wrapper(rather than using pointers):

目前的类型neighbours将存储对象的副本,而不是引用。如果你真的想存储引用,我建议使用a std::reference_wrapper(而不是使用指针):

vector<reference_wrapper<Site>> neighbors;

回答by Lightness Races in Orbit

vector<Site> myStack();

This is wrong. Lose the ().

这是错误的。失去().

You're declaring a function, not a vector.

你声明的是一个函数,而不是一个向量。

Just write:

写就好了:

vector<Site> myStack;

回答by Rodrigo Saucedo

You could use:

你可以使用:

vector<Site> myStack;
myStack.resize(100); //will create 100 <Site> objects