本文整理汇总了C++中SList::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ SList::insert方法的具体用法?C++ SList::insert怎么用?C++ SList::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SList
的用法示例。
在下文中一共展示了SList::insert方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
SList<int> sl;
sl.insert_first(1);
sl.display();
sl.insert_last(2);
sl.display();
sl.insert_last(3);
sl.display();
sl.remove_last();
sl.display();
sl.remove_first();
sl.display();
cout << sl.find(2) << endl;
sl.insert(4, sl.find(2));
sl.display();
sl.remove(sl.find(4));
sl.display();
sl.remove_all();
return 0;
}
示例2: main
int main(void){
SList<int> list;
SList<int>::iterator i;
list.insert(5);
list.insert(6);
list.insert(1);
list.insert(2);
// list.append(10);
// list.rmFirst();
// list.rmLast();
for(i=list.begin();i!=list.end();i++){
cout << *i << endl;
}
for(i=list.begin();i!=list.end();i++){
*i = *i + 1;
}
for(i=list.begin();i!=list.end();i++){
cout << *i << endl;
}
}
示例3: main
int main() {
SList<int> *intList = new SList<int>;
delete intList;
SList<string> facultyList;
facultyList.insert("unknown");
facultyList.insert("erdly");
facultyList.insert("sung");
facultyList.insert("olson");
facultyList.insert("zander");
facultyList.insert("berger");
facultyList.insert("cioch");
facultyList.insert("fukuda");
facultyList.insert("stiber");
facultyList.insert("jackels");
cout << "#faculty members: " << facultyList.size() << endl;
facultyList.show();
cout << endl;
cout << "deleting unknown" << endl;
facultyList.remove("unknown");
cout << "#faculty members: " << facultyList.size() << endl;
facultyList.show();
cout << endl;
cout << "finding stiber = " << facultyList.find("stiber") << endl;
cout << endl;
cout << "create another list" << endl;
SList<string> studentList = facultyList;
cout << "finding stiber = " << facultyList.find("stiber") << endl;
cout << "#faculty members: " << facultyList.size() << endl;
cout << endl;
cout << "cost of find = " << facultyList.getCost() << endl;
}
示例4: unittest
/*
* Unit testing functions. Do not alter.
*/
void unittest ()
{
cout << "\nSTARTING UNIT TEST\n\n";
SList list;
try {
btassert<bool>(list.getSize() == 0);
cout << "Passed TEST 1: default constructor (size) \n";
} catch (bool b) {
cout << "# FAILED TEST 1: default constructor (size) #\n";
}
try {
btassert<bool>(list.toString() == "");
cout << "Passed TEST 2: toString \n";
} catch (bool b) {
cout << "# FAILED TEST 2: toString #\n";
}
list.insert(10);
try {
btassert<bool>(list.getSize() == 1 && list.toString() == "10");
cout << "Passed TEST 3: insert(10)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 3: insert(10)/getSize/toString #\n";
}
list.insert(50);
try {
btassert<bool>(list.getSize() == 2 && list.toString() == "10,50");
cout << "Passed TEST 4: insert(50)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 4: insert(50)/getSize/toString #\n";
}
list.insert(30);
try {
btassert<bool>(list.getSize() == 3 && list.toString() == "10,30,50");
cout << "Passed TEST 5: insert(30)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 5: insert(30)/getSize/toString #\n";
}
list.insert(5);
try {
btassert<bool>(list.getSize() == 4 && list.toString() == "5,10,30,50");
cout << "Passed TEST 6: insert(5)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 6: insert(5)/getSize/toString #\n";
}
list.insert(55);
try {
btassert<bool>(list.getSize() == 5 && list.toString() == "5,10,30,50,55");
cout << "Passed TEST 7: insert(55)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 7: insert(55)/getSize/toString #\n";
}
list.insert(20);
try {
btassert<bool>(list.getSize() == 6 && list.toString() == "5,10,20,30,50,55");
cout << "Passed TEST 8: insert(20)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 8: insert(20)/getSize/toString #\n";
}
list.insert(40);
try {
btassert<bool>(list.getSize() == 7 && list.toString() == "5,10,20,30,40,50,55");
cout << "Passed TEST 9: insert(40)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 9: insert(40)/getSize/toString #\n";
}
list.insert(30);
try {
btassert<bool>(list.getSize() == 8 && list.toString() == "5,10,20,30,30,40,50,55");
cout << "Passed TEST 10: insert(30)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 10: insert(30)/getSize/toString #\n";
}
list.insert(5);
try {
btassert<bool>(list.getSize() == 9 && list.toString() == "5,5,10,20,30,30,40,50,55");
cout << "Passed TEST 11: insert(5)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 11: insert(5)/getSize/toString #\n";
}
try {
btassert<bool>(list.removeFirst(1) == false);
cout << "Passed TEST 12: removeFirst(1) \n";
} catch (bool b) {
cout << "# FAILED TEST 12: removeFirst(1) #\n";
//.........这里部分代码省略.........
示例5: main
int main( int argc, char* argv[] ) {
// verify argument
if ( argc != 2 ) {
cerr << "usage: statistics size" << endl;
return -1;
}
// verify size
int size = atoi( argv[1] );
if ( size < PATTERN_MAX ) {
cerr << "usage: size >= " << PATTERN_MAX << endl;
return -1;
}
// initialize list items
srand( 1 );
int *items = new int[size];
initArray( items, size, -1 );
printArray( items, size, "items" );
// initialize access pattern
int *pattern = new int[PATTERN_MAX];
initArray( pattern, PATTERN_MAX, size );
printArray( pattern, PATTERN_MAX, "pattern" );
// initialize pattern frequency
int *frequency = new int[PATTERN_MAX];
for ( int i = 1; i < PATTERN_MAX; i++ )
frequency[i] = i + frequency[i - 1];
printArray( frequency, PATTERN_MAX, "frequency" );
// generate access sequence
int *sequence = new int[SEQ_MAX];
for ( int i = 0; i < SEQ_MAX; i++ ) {
int random = rand( ) % ( frequency[PATTERN_MAX - 1] + 1 );
int hit;
for ( hit = 0; hit < PATTERN_MAX; hit++ ) {
if ( random <= frequency[hit] ) {
break;
}
}
sequence[i] = items[pattern[hit]];
}
printArray( sequence, SEQ_MAX, "sequence" );
// now conduct performance evaluation
// doubly linked list
DList<int> dlist;
for ( int i = 0; i < size; i++ )
dlist.insert( items[i], i );
for ( int i = 0; i < SEQ_MAX; i++ )
dlist.find( sequence[i] );
cout << "dlist's find cost = " << dlist.getCost( ) << endl;
// mtf list
MtfList<int> mtflist;
for ( int i = 0; i < size; i++ )
mtflist.insert( items[i], i );
for ( int i = 0; i < SEQ_MAX; i++ )
mtflist.find( sequence[i] );
cout << "mtflist's find cost = " << mtflist.getCost( ) << endl;
// transpose list
TransposeList<int> translist;
for ( int i = 0; i < size; i++ )
translist.insert( items[i], i );
for ( int i = 0; i < SEQ_MAX; i++ )
translist.find( sequence[i] );
cout << "translist's find cost = " << translist.getCost( ) << endl;
// skip list
SList<int> skiplist;
for ( int i = 0; i < size; i++ )
skiplist.insert( items[i] );
for ( int i = 0; i < SEQ_MAX; i++ )
skiplist.find( sequence[i] );
cout << "skip's find cost = " << skiplist.getCost( ) << endl;
return 0;
}
示例6: unittest
/*
* Unit testing functions. Do not alter.
*/
void unittest ()
{
cout << "\nSTARTING UNIT TEST\n\n";
SList<int> intList;
cout << "INTEGER LIST TEST\n\n";
try {
btassert<bool>(intList.getSize() == 0);
cout << "Passed TEST 1: default constructor (size) \n";
} catch (bool b) {
cout << "# FAILED TEST 1: default constructor (size) #\n";
}
try {
btassert<bool>(intList.toString() == "");
cout << "Passed TEST 2: toString \n";
} catch (bool b) {
cout << "# FAILED TEST 2: toString #\n";
}
intList.insert(10);
try {
btassert<bool>(intList.getSize() == 1 && intList.toString() == "10");
cout << "Passed TEST 3: insert(10)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 3: insert(10)/getSize/toString #\n";
}
intList.insert(10);
try {
btassert<bool>(intList.getSize() == 2 && intList.toString() == "10,10");
cout << "Passed TEST 4: insert(10)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 4: insert(10)/getSize/toString #\n";
}
intList.insert(30);
try {
btassert<bool>(intList.getSize() == 3 && intList.toString() == "10,10,30");
cout << "Passed TEST 5: insert(30)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 5: insert(30)/getSize/toString #\n";
}
intList.insert(30);
try {
btassert<bool>(intList.getSize() == 4 && intList.toString() == "10,10,30,30");
cout << "Passed TEST 6: insert(30)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 6: insert(30)/getSize/toString #\n";
}
intList.insert(15);
try {
btassert<bool>(intList.getSize() == 5 && intList.toString() == "10,10,15,30,30");
cout << "Passed TEST 7: insert(15)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 7: insert(15)/getSize/toString #\n";
}
intList.insertHead(5);
try {
btassert<bool>(intList.getSize() == 6 && intList.toString() == "5,10,10,15,30,30");
cout << "Passed TEST 8: insertHead(5)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 8: insertHead(5)/getSize/toString #\n";
}
intList.insertTail(50);
try {
btassert<bool>(intList.getSize() == 7 && intList.toString() == "5,10,10,15,30,30,50");
cout << "Passed TEST 9: insertTail(50)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 9: insertTail(50)/getSize/toString #\n";
}
try {
btassert<bool>(intList.removeFirst(1) == false);
cout << "Passed TEST 10: removeFirst(1) \n";
} catch (bool b) {
cout << "# FAILED TEST 10: removeFirst(1) #\n";
}
try {
btassert<bool>(intList.removeAll(100) == false);
cout << "Passed TEST 11: removeAll(1) \n";
} catch (bool b) {
cout << "# FAILED TEST 11: removeAll(1) #\n";
}
try {
btassert<bool>(intList.removeFirst(10) == true && intList.getSize() == 6 && intList.toString() == "5,10,15,30,30,50");
cout << "Passed TEST 12: removeFirst(10)/getSize/toString \n";
} catch (bool b) {
cout << "# FAILED TEST 12: removeFirst(10)/getSize/toString #\n";
//.........这里部分代码省略.........