本文整理汇总了C++中StaticArray::keys方法的典型用法代码示例。如果您正苦于以下问题:C++ StaticArray::keys方法的具体用法?C++ StaticArray::keys怎么用?C++ StaticArray::keys使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StaticArray
的用法示例。
在下文中一共展示了StaticArray::keys方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
// print my name and this assignment's title
cout << "Lab 3b, The \"Write A Static Array Class Template\" Program \n";
cout << "Programmer: Licong Wang\n";
cout << "Editor(s) used: Visual studio 2013\n";
cout << "Compiler(s) used: Microsoft c++ complier\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
StaticArray<int, 100> a;
string buf;
int key;
int value;
while (true)
{
cout << "Input an index and a value [Q to quit]: ";
cin >> buf;
if (buf == "Q" || buf == "q")
break;
key = atoi(buf.c_str());
cin >> buf;
value = atoi(buf.c_str());
a[key] = value;
}
cout << "\nI stored this many values : " << a.size() << endl;
cout << "These values are:" << endl;
vector<int> keys = a.keys();
for (int i = 0; i < keys.size(); i++)
cout << keys[i] << " " << a[keys[i]] << endl;
while (true)
{
cout << "Input an index for me to look up [Q to quit]: ";
cin >> buf;
if (buf == "Q" || buf == "q")
break;
else
{
key = atoi(buf.c_str());
if (a.containsKey(key))
cout << "Found it -- the value stored at " << key << " is " << a[key] << endl;
else
cout << "I didn't find it" << endl;
}
}
cout << "\nPress ENTER to continue.\n";
cin.ignore(); // prevent cin.get() from getting a new line from previous cin
cin.get();
}
示例2: main
int main()
{
// print my name and this assignment's title
cout << "LAB 3a: Write A Static Array Class Template\n";
cout << "Programmer: Jacky Chow\n";
cout << "Editor(s) used: Notepad++\n";
cout << "Compiler(s) used: Visual C++\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
vector<int> k;
StaticArray<int, 10> a;
//Test capacity() and size()
cout << "Capacity of a should be 10\nCapacity returns " << a.capacity() << endl;
assert(10 == a.capacity());
cout << "Size should be 0\nSize returns " << a.size() << endl;
assert(0 == a.size());
//Test kes()
k = a.keys();
cout << "Keys of a in use: ";
for(int i = 0; i < k.size(); i++)
cout << k[i] << " ";
cout << endl << endl;
//Test operator[] setter and getter
a[3] = 7;
cout << "a[3] was set to 7\na[3] is returns " << a[3] << endl;
assert(7 == a[3]);
cout << "Size should be 1\nSize returns " << a.size() << endl;
assert(1 == a.size());
k = a.keys();
cout << "Keys of a in use: ";
for(int i = 0; i < k.size(); i++)
cout << k[i] << " ";
cout << endl << endl;
a[5] = 20;
cout << "a[5] was set to 20\na[5] is returns " << a[5] << endl;
assert(20 == a[5]);
a[7] = 15;
cout << "a[7] was set to 15\na[7] is returns " << a[7] << endl;
assert(15 == a[7]);
a[9] = 3;
cout << "a[9] was set to 3\na[9] is returns " << a[9] << endl;
assert(3 == a[9]);
k = a.keys();
cout << "Keys of a in use: ";
for(int i = 0; i < k.size(); i++)
cout << k[i] << " ";
cout << endl << endl;
//Test containsKey()
cout << "\nKey 1 is currently not in use.\n";
if(a.containsKey(1)) cout << "Error, a[1] is not is use\n";
else
cout << "containsKey() on a[1] returned a false\n";
assert(!(a.containsKey(1)));
a[1] = 8;
cout << "a[1] was set to 8\na[1] is returns " << a[1] << endl;
assert(8 == a[1]);
cout << "Key 1 is now in use.\n";
if(a.containsKey(1)) cout << "containsKey() on a[1] now returns true\n";
else
cout << "Error! a[1] did not return a true\n";
assert(a.containsKey(1));
k = a.keys();
cout << "Keys of a in use: ";
for(int i = 0; i < k.size(); i++)
cout << k[i] << " ";
cout << endl << endl;
//Test deleteKey()
a.deleteKey(1);
cout << "a[1] was deleted\n";
cout << "Key 1 is again not in use.\n";
if(a.containsKey(1)) cout << "Error, a[1] is not is use\n";
else
cout << "a[1] returned a false\n";
k = a.keys();
cout << "Keys of a in use: ";
for(int i = 0; i < k.size(); i++)
cout << k[i] << " ";
cout << endl << endl;
//Cout values in current inUse keys
for(int i = 0; i < k.size(); i++)
{
cout << "a[" << k[i] << "] = " << a[k[i]] << endl;
}
// object copy testing
{
const StaticArray<int, 10> copy = a; // a read-only copy
cout << endl;
k = copy.keys();
for(int i = 0; i < k.size(); i++)
//.........这里部分代码省略.........