2010-12-11 12 views
7

में स्ट्रिंग्स के स्थिर संग्रह को कैसे कार्यान्वित करना चाहिए, मैं सी ++ के लिए बहुत नया हूं इसलिए यह उत्तर देने का एक आसान सवाल हो सकता है। मैं एक वर्ग (व्यक्ति) लिख रहा हूं और जब कोई व्यक्ति बनाया जाता है तो इसे पूर्वनिर्धारित नामों के संग्रह से यादृच्छिक नाम असाइन किया जाना चाहिए। तो व्यक्ति वर्ग के भीतर मैं तारों के कुछ प्रकार के स्थैतिक संग्रह को परिभाषित करना चाहता हूं जिन्हें मैं यादृच्छिक रूप से एक्सेस कर सकता हूं और इसलिए मुझे यह जानने की भी आवश्यकता होगी कि कितने हैं।मुझे अपनी कक्षा

मैं यहां क्यूटी का भी उपयोग कर रहा हूं, इसलिए समाधान मानक लाइब्रेरी या क्यूटी लाइब्रेरी से चीजों का उपयोग करना चाहिए।

मैं एक जावा पृष्ठभूमि से हूँ और जावा में मैं शायद की तरह कुछ करना होगा:

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

क्या इस मामले में बराबर होगा?

उत्तर

21

आप QStringList का उपयोग कर सकते हैं।

Person.h:

class Person 
{ 
private: 
    static QStringList names; 
}; 

Person.cpp:

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

मान लिया जाये कि सी ++ 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]); 

सी मान लिया जाये कि ++ 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* के बजाय टाइप कर सकते हैं।

+0

जवाब के लिए धन्यवाद, यह लेने के लिए एक स्वीकृत जवाब दिया है कि वे ठीक हो मुश्किल था, लेकिन मैं सिर्फ QStringList साथ चला गया पूरी तरह उत्तर दें क्योंकि यह अच्छी तरह से इस तथ्य के साथ फिट बैठता है कि मैं क्यूटी सीखने की कोशिश कर रहा हूं। – DaveJohnston

+0

@ डेव: हालांकि मैं आपको समझ सकता हूं, फिर भी मैं जहां भी संभव हो, अधिक मानक और पोर्टेबल कोड लिखना पसंद करता हूं। भले ही यह इतना साफ न हो (सी ++ 03 उदाहरण)। – ybungalobill

4

सबसे पहले, एक स्थिर सरणी से यादृच्छिक नाम पैदा करने के लिए एक बहुत ही सरल कार्यक्रम। उचित वर्ग कार्यान्वयन और नीचे पाया जा सकता है।

#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; } 

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; 
} 
+0

एक अच्छी व्याख्या, लेकिन 'नामस्थान std का उपयोग करना;' एक बुरा विचार है और समस्याओं का पूरा समूह बन सकता है, जिनमें से कई नामस्थान संघर्षों के कारण SO पर पोस्ट किए गए हैं।'Std ::' टाइप करने में अगली-टू-नो-टाइम लगती है। –

+3

"हेडस्पेस std;" का उपयोग कर "हेडर फ़ाइल * में खराब है *। स्रोत फ़ाइल में इसका उपयोग करना बिल्कुल ठीक है। –

+0

हालांकि, स्वाभाविक रूप से, बहस ':' पर ​​बहती है, http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-cononsidered-a-bad-practice-in-c –

संबंधित मुद्दे