本文整理汇总了C++中StaticArray::capacity方法的典型用法代码示例。如果您正苦于以下问题:C++ StaticArray::capacity方法的具体用法?C++ StaticArray::capacity怎么用?C++ StaticArray::capacity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StaticArray
的用法示例。
在下文中一共展示了StaticArray::capacity方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
// this code block prints my name and the assignment's title
cout << "Lab 3b, My Static Array \n";
cout << "Programmer: Nicholas Tie\n";
cout << "Editor(s) used: Codeblocks\n";
cout << "Compiler(s) used: GNU\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
// declare variables
StaticArray<int,100> sArray;
int c = sArray.capacity();
char buff[MAX_CHARS_PER_LINE] = {};
// User input
for (int i=0; i < c; i++){
cout << "Input an index and a value [Q to quit]: ";
cin.getline(buff, MAX_CHARS_PER_LINE);
// parse input into tokens
int n = 0;
token[0] = strtok(buff, DELIMITER); // first token
if (token[0]){ // zero if line is blank
for (n = 1; n < MAX_TOKENS_PER_LINE; n++){
token[n] = strtok(0, DELIMITER); // subsequent tokens
if (!token[n]) break; // no more tokens
}
}
if (buff[0] == 'Q' || buff[0] == 'q'){
break;
}
else{
int a = atoi(token[0]);
int b = atoi(token[1]);
sArray[a] = b;
}
} // end input
// # Values stored:
cout << endl << "I stored this many values: " << sArray.size() <<endl;
// List of all entered values:
cout << "\nThe values inputted are: " << endl;
for(int j = 0; j <= c; j++){
if(sArray.containsKey(j))
cout << j << " : " << sArray[j] << endl;
}
// Index Lookup
string buf;
for (int i=0; i < c; i++){
cout << endl << "Input an index for me to look up [Q to quit]: ";
cin >> buf;
int x = atoi(buf.c_str());
if (buf == "Q" || buf == "q"){
cout << endl << "!*****Program has been Terminated*****!" << endl;
break;
}
else if(sArray.containsKey(x)){
cout << "Value stored at " << x << " is "
<< sArray[x] << endl;
}
else{
cout << "I couldn't find it!" << endl;
}
}
}//end main
示例2: capacity
unsigned capacity() const {
return x.capacity();
}
示例3: 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++)
//.........这里部分代码省略.........