我应该如何在我的类中实现静态字符串集合

发布于 2024-10-06 19:38:36 字数 323 浏览 3 评论 0原文

我对 C++ 很陌生,所以这可能是一个很容易回答的问题。我正在编写一个类(Person),当创建一个Person时,应该从预定义名称的集合中为它分配一个随机名称。因此,在 Person 类中,我想定义某种可以随机访问的静态字符串集合,因此我还需要知道有多少个字符串。

我在这里也使用 Qt,因此解决方案最好使用标准库或 Qt 库中的内容。

我有 Java 背景,在 Java 中我可能会做类似的事情:

private static final String[] NAMES = { "A", "B" };

在这种情况下等效的是什么?

I am very new to C++ so this might be an easy question to answer. I am writing a class (Person) and when a Person is created it should be assigned a random name from a collection of predefined names. So within the Person class I would like to define some sort of static collection of strings that I can access randomly and therefore I would also need to know how many there are.

I am using Qt here too, so the solution should preferably be using things from the standard library or the Qt library.

I am from a Java background and in Java I would probably do something like:

private static final String[] NAMES = { "A", "B" };

What would be the equivalent in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您可以使用 QStringList

Person.h:

class Person
{
private:
    static QStringList names;
};

Person.cpp:

QStringList Person::names = QStringList() << "Arial" << "Helvetica" 
    << "Times" << "Courier";

You could use QStringList.

Person.h:

class Person
{
private:
    static QStringList names;
};

Person.cpp:

QStringList Person::names = QStringList() << "Arial" << "Helvetica" 
    << "Times" << "Courier";
江心雾 2024-10-13 19:38:36

假设 C++03:

class YourClass {
    static const char*const names[];
    static const size_t namesSize;
};

// in one of the translation units (*.cpp)
const char*const YourClass::names[] = {"A", "B"};
const size_t YourClass::namesSize = sizeof(YourClass::names) / sizeof(YourClass::names[0]);

假设 C++0x:

class YourClass {
    static const std::vector<const char*> names;
};

// in one of the translation units (*.cpp)
const vector<const char*> YourClass::names = {"A", "B"};

当然,您可以使用您最喜欢的字符串类型来代替 const char*

Assuming C++03:

class YourClass {
    static const char*const names[];
    static const size_t namesSize;
};

// in one of the translation units (*.cpp)
const char*const YourClass::names[] = {"A", "B"};
const size_t YourClass::namesSize = sizeof(YourClass::names) / sizeof(YourClass::names[0]);

Assuming C++0x:

class YourClass {
    static const std::vector<const char*> names;
};

// in one of the translation units (*.cpp)
const vector<const char*> YourClass::names = {"A", "B"};

And of course you can use your favorite string type instead of const char*.

暮倦 2024-10-13 19:38:36

首先,一个非常简单的程序,用于从静态数组生成随机名称。正确的类实现可以在下面找到。

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

// import the std namespace (to avoid having to use std:: everywhere)
using namespace std;
// create a constant array of strings
static string const names[] = { "James", "Morrison",
                                "Weatherby", "George", "Dupree" };
// determine the number of names in the array
static int const num_names = sizeof(names)/sizeof(names[0]);

// declare the getRandomName() function
string getRandomName();

// standard main function
int main (int argc, char * argv[])
{
    // seed the random number generator
    srand(time(0));
    // pick a random name and print it
    cout << getRandomName() << endl;
    // return 0 (no error)
    return 0;
}

// define the getRandomName() function
string getRandomName()
{
    // pick a random name (% is the modulo operator)
    return names[rand()%num_names];
}

类实现

Person.h

#ifndef PERSON_
#define PERSON_

#include <string>

class Person
{
    private:
        std::string p_name;
    public:
        Person();
        std::string name();
};

#endif

Person.cpp

#include "Person.h"
#include <stdlib.h>

using namespace std;

static string const names[] = { "James", "Morrison",
                                "Weatherby", "George", "Dupree" };
static int const num_names = sizeof(names)/sizeof(names[0]);

Person::Person() : p_name(names[rand()%num_names]) { }
string Person::name() { return p_name; }

ma​​in.cpp

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Person.h"

using namespace std;

int main (int argc, char * argv[])
{
    // seed the random number generator
    srand(time(0));

    // create 3 Person instances
    Person p1, p2, p3;

    // print their names
    cout << p1.name() << endl;
    cout << p2.name() << endl;
    cout << p3.name() << endl;

    // return 0 (no error)
    return 0;
}

First, a very simple program for generating random names from a static array. The proper class implementation can be found further down.

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

// import the std namespace (to avoid having to use std:: everywhere)
using namespace std;
// create a constant array of strings
static string const names[] = { "James", "Morrison",
                                "Weatherby", "George", "Dupree" };
// determine the number of names in the array
static int const num_names = sizeof(names)/sizeof(names[0]);

// declare the getRandomName() function
string getRandomName();

// standard main function
int main (int argc, char * argv[])
{
    // seed the random number generator
    srand(time(0));
    // pick a random name and print it
    cout << getRandomName() << endl;
    // return 0 (no error)
    return 0;
}

// define the getRandomName() function
string getRandomName()
{
    // pick a random name (% is the modulo operator)
    return names[rand()%num_names];
}

Class implementation

Person.h

#ifndef PERSON_
#define PERSON_

#include <string>

class Person
{
    private:
        std::string p_name;
    public:
        Person();
        std::string name();
};

#endif

Person.cpp

#include "Person.h"
#include <stdlib.h>

using namespace std;

static string const names[] = { "James", "Morrison",
                                "Weatherby", "George", "Dupree" };
static int const num_names = sizeof(names)/sizeof(names[0]);

Person::Person() : p_name(names[rand()%num_names]) { }
string Person::name() { return p_name; }

main.cpp

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Person.h"

using namespace std;

int main (int argc, char * argv[])
{
    // seed the random number generator
    srand(time(0));

    // create 3 Person instances
    Person p1, p2, p3;

    // print their names
    cout << p1.name() << endl;
    cout << p2.name() << endl;
    cout << p3.name() << endl;

    // return 0 (no error)
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文