c ++ - 设置一个接受值列表的数组

时间:2012-08-06 10:30:46

标签: c++ wxwidgets

如何设置以下内容:

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之类的东西,所以这个数字数组包含1-9的所有内容,不包括0。

由于

2 个答案:

答案 0 :(得分: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]);

答案 1 :(得分: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";
            }
        }
};

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