2012-08-06 34 views
1

列表如何设置下面的数组:C++ - 设置接受值

wxArrayString numberArray; 
numberArray.Add(wxT("1")); 
numberArray.Add(wxT("2")); 
numberArray.Add(wxT("3")); 
numberArray.Add(wxT("4")); 
numberArray.Add(wxT("5")); 
numberArray.Add(wxT("6")); 
numberArray.Add(wxT("7")); 
numberArray.Add(wxT("8")); 
numberArray.Add(wxT("9")); 

并不像1-9每件事写specificlly但东西,所以这个数字阵列一切从1-9 ,不包括0。

感谢

+0

这是一个unicode构建,非unicode,或两者兼而有之? – 2012-08-06 12:03:41

回答

2
// Add numbers 1-9 to numberArray 
wxArrayString numberArray; 

for (size_t i = 1; i <= 9; ++i) 
    numberArray.Add(wxString::Format(wxT("%d"), i)); 

// Display content of numberArray 
for (size_t i = 0; i < numberArray.size(); ++i) 
    wxLogDebug(numberArray[i]); 
0

如果我理解正确的,你想数组只接受特定的一组数据?如果你,你可以创建一个这样的类:

class MyArray 
{ 
    //your accepted data will be stored in this vector. 
    std::vector<int> data; 

    //the acceptable values will be stored in this set. 
    std::set<int> acceptable; 

    public: 
     MyArray() 
     { 
      // in the constructor we fill the set. 
      for(int i=0; i<=10; i++) 
       acceptable.insert(i); 
     } 
     void add(int item) 
     { 
      // if the set contains the item you want to insert, then insert it 
      if(acceptable.find(item) != acceptable.end()) 
      { 
       data.push_back(item); 
       std::cout<<"Added"; 
      } 
      // else throw error or simply don't add it. 
      else 
      { 
       std::cout<<"Not acceptable"; 
      } 
     } 
}; 

如果我完全误解了你,那么对不起!只要告诉我,如果它不相关,我会删除答案!

+0

将它与标题一起写入 – 2012-08-06 11:37:20

+0

您是否需要在单独的标题/ cpp版本中使用此代码? – 2012-08-06 11:45:08