本文整理汇总了C++中BST::begin方法的典型用法代码示例。如果您正苦于以下问题:C++ BST::begin方法的具体用法?C++ BST::begin怎么用?C++ BST::begin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BST
的用法示例。
在下文中一共展示了BST::begin方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(){
BST bst;
bst.insert(1);
bst.insert(3);
bst.insert(2);
for (BST::iterator i = bst.begin(); i != bst.end(); ++i){
cout << *i << endl;
}
}
示例2: main
int main(){
BST<int> bst;
bst.add(2);
bst.add(1);
bst.add(3);
bst.add(13);
bst.add(31);
for (BST<int>::iterator i = bst.begin(); i != bst.end(); ++i){
cout << *i << endl;
}
}
示例3: BTSort
void BTSort(arrayItems D[])
{
BST<arrayItems> sortBin;
// insert data to BST
for (int i = 0; i < NUMITEMS; i++)
sortBin.insert(D[i]);
int count = 0;
BSTiterator<arrayItems> it;
// copy back to the array
for (BSTiterator<arrayItems> it = sortBin.begin(); it != sortBin.end(); it++)
{
D[count] = * it;
count++;
}
}
示例4: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2012
* Author: P. Kube (c) 2012
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(100);
v.push_back(-33);
v.push_back(2);
v.push_back(10);
v.push_back(-3);
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return true
if(! b.insert(*vit) ) {
cout << "Incorrect return value when inserting " << *vit << endl;
return -1;
}
}
vit = v.begin();
for(; vit != ven; ++vit) {
// all these inserts are duplicates, so should return false
if( b.insert(*vit) ) {
cout << "Incorrect return value when re-inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "Size is: " << b.size() << endl;
if(b.size() != v.size()) {
cout << "... which is incorrect." << endl;
return -1;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; vit != ven; ++vit) {
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++it;
}
cout << "OK." << endl;
}
示例5: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2010
* Author: P. Kube (c) 2010
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
map<int, bool> m;
for (int i = 0; i < 50; ++i) {
int n = rand() % 1000;
if (!m[n]) {
m[n] = true;
v.push_back(n);
}
}
v.erase(unique(v.begin(), v.end()), v.end());
std::cout << "v is size: " << v.size() << std::endl;
for (size_t i = 0; i < v.size(); ++i) {
//std::cout << v[i] << std::endl;
}
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return true
if(! b.insert(*vit) ) {
cout << "Incorrect return value when inserting " << *vit << endl;
return -1;
}
}
for(; vit != ven; ++vit) {
// all these inserts are duplicates, so should return false
if( b.insert(*vit) ) {
cout << "Incorrect return value when re-inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "Size is: " << b.size() << endl;
if(b.size() != v.size()) {
cout << "... which is incorrect." << endl;
return -1;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; it != en; ++it) {
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++vit;
}
}
示例6: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2013
* Author: P. Kube (c) 2013
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(33);
v.push_back(66);
v.push_back(144);
v.push_back(1984);
v.push_back(-92393);
v.push_back(46);
v.push_back(22);
v.push_back(0);
v.push_back(123445);
v.push_back(100);
v.push_back(-33);
/*
vector<string> v;
v.push_back("hi");
v.push_back("hi no");
v.push_back("hi yes");
v.push_back("hi you");
v.push_back("hi fly");
v.push_back("hi w");
*/
/* Create an instance of BST holding int */
BST<int> b;
// BST<string> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
// if(! pr.second ) {
// cout << "Incorrect bool return value when inserting " << *vit << endl;
// return -1;
// }
if(*(pr.first) != *vit) {
cout << "Incorrect iterator return value when inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "" << endl;
cout << "Size is: " << b.size() << endl;
cout << "" << endl;
if(b.size() != 14) {
cout << "... which is incorrect." << endl;
return -1;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* This is for testing find on something not in the BST
* Causes a seg fault so it is taken out
BST<int>::iterator it3 = b.find(5);;
cout << *it3 << endl;
*/
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
cout << "" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; vit != ven; ++vit) {
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++it;
}
cout << "" << endl;
cout << "OK, order is correct" << endl;
cout << "" << endl;
//.........这里部分代码省略.........
示例7: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2013
* Author: P. Kube (c) 2013
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(100);
v.push_back(-33);
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
cout << "vit = " << *vit << endl;
if(! pr.second ) {
cout << "Incorrect bool return value when inserting " << *vit << endl;
return -1;
}
if(*(pr.first) != *vit) {
cout << "Incorrect iterator return value when inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "Size is: " << b.size() << endl;
if(b.size() != v.size()) {
cout<< "bsize is" << b.size() << endl;
cout<< "vsize is" << v.size() << endl;
cout << "... which is incorrect." << endl;
return -1;
}
cout<<"simple test" << endl;
for(BST<int>::iterator it = b.begin(); it != b.end(); ++it){
cout<<*it<< endl;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; vit != ven; ++vit) {
if(! (it != en) ) {
cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
return -1;
}
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++it;
}
cout << "OK." << endl;
/* Testing clear and checking clear size afterwards*/
BST<int> c;
vit = v.begin();
ven = v.end();
for(vit = v.begin(); vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = c.insert(*vit);
}
c.clear();
cout<< "clear size is" << c.size() << endl;
if(c.size() != 0){
cout << "fail" << endl;
return -1;
}
/* Testing BST destructor */
BST<int>* d = new BST<int>();
d->insert(3);
//.........这里部分代码省略.........
示例8: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2013
* Author: P. Kube (c) 2013
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
vector<int> random;
v.push_back(50);
v.push_back(60);
v.push_back(30);
v.push_back(33);
v.push_back(38);
v.push_back(35);
v.push_back(37);
v.push_back(20);
// v.push_back(15);
// Overriding for more extensive tests */
/*
srand(time(NULL));
for (int i = 0; i < 10000; i++ )
{
int r = rand() % 10000;
bool dupe = false;
vector<int>::iterator ranit = random.begin();
vector<int>::iterator ranen = random.end();
for(; ranit != ranen; ++ranit)
{
if (*ranit == r)
{
//cout << "dupe" << endl;
dupe = true;
// return -1;
}
}
if (!dupe)
{
cout << r << endl;
random.push_back(r);
v.push_back(r);
}
}
*/
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
if(! pr.second ) {
cout << "Incorrect bool return value when inserting " << *vit << endl;
return -1;
}
if(*(pr.first) != *vit) {
cout << "Incorrect iterator return value when inserting " << *vit << endl;
return -1;
}
}
/* Test inserting repeated values - expected false result */
/*
vector<int> z;
z.push_back(1);
z.push_back(3);
vector<int>::iterator bit = z.begin();
vector<int>::iterator ben = z.end();
for(; bit !=ben; ++bit) {
std::pair<BST<int>::iterator,bool> pr = b.insert(*bit);
if( pr.second ) {
cout << "Incorrect bool return value when inserting " << *bit << endl;
return -1;
}
if(*(pr.first) != *bit) {
cout << "Incorrect iterator return value when inserting " << *bit << endl;
return -1;
}
}
// Make sure size hasn't changed for failed inputs.
cout << "Size is: " << b.size() << endl;
if(b.size() == z.size()) {
cout << "... which is incorrect." << endl;
return -1;
}
*/
/* Test size. */
//.........这里部分代码省略.........
示例9: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2013
* Author: P. Kube (c) 2013
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
/*
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(100);
v.push_back(-33);
v.push_back(-20);
v.push_back(6);
v.push_back(0);
v.push_back(-1);
v.push_back(-2);
v.push_back(-3);
v.push_back(-4);
*/
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
if(! pr.second ) {
cout << "Incorrect bool return value when inserting " << *vit << endl;
return -1;
}
if(*(pr.first) != *vit) {
cout << "Incorrect iterator return value when inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "Size is: " << b.size() << endl;
if(b.size() != v.size()) {
cout << "... which is incorrect." << endl;
return -1;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; vit < ven; ++vit) {
if(! (it != en) ) {
cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
return -1;
}
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++it;
}
/** Added a basic clear tests that checks for correct size after delete */
cout << "OK." << endl;
b.clear();
cout << ((b.size() == 0)
? "PASS: clear() size of tree: "
: "FAIL: clear() Size of tree: ") << b.size() << endl;
/** ================================ Added test Cases =====================*/
/* Test fixtures */
vector<int> v2;
srand(unsigned(time(NULL)));
BST<int> b2;
BST<int>* treeDestructorTest = new BST<int>();
unsigned int numInspected = 0;
unsigned int sizeOfInput = 0;
for(int i = 1000; i >= -1000; i--){
v2.push_back(i);
//.........这里部分代码省略.........
示例10: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2013
* Author: P. Kube (c) 2013
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(100);
v.push_back(-33);
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
if(! pr.second ) {
cout << "Incorrect bool return value when inserting " << *vit << endl;
return -1;
}
if(*(pr.first) != *vit) {
cout << "Incorrect iterator return value when inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "Size is: " << b.size() << endl;
if(b.size() != v.size()) {
cout << "... which is incorrect." << endl;
return -1;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; vit != ven; ++vit) {
if(! (it != en) ) {
cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
return -1;
}
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++it;
}
cout << "OK." << endl;
}
示例11: main
/**
* A simple partial test driver for the RST class template.
* P2 CSE 100
* Author: P. Kube (c) 2010, 2013
*/
int main(int argc, char** argv) {
int N = 1000;
if(argc > 1) N = atoi(argv[1]);
// a good RST implementation should need no more than this number
// of key comparisons when inserting N keys
double maxcompsperkey = (log(N) * 2.5);
cout << "Log(n) is: " << log(N) << endl;
/* Create an STL vector of some countints, in sorted order */
vector<countint> v;
for(int i=0; i<N; i++) {
// v.push_back(i);
v.push_back(i);
}
/* Create an empty instance of RST holding countint */
BST<countint>* r = new RST<countint>();
/* Clear the comparison counter */
countint::clearcount();
/* Insert the data items, in order */
cout << "Inserting " << N << " sorted keys in initially empty RST...";
vector<countint>::iterator vit = v.begin();
vector<countint>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<countint>::iterator,bool> pr = r->insert(*vit);
if(! pr.second ) {
cout << "Incorrect bool return value when inserting " << *vit << endl;
return -1;
}
// countint rc = *(pr.first);
// countint bc = *vit;
// if(rc < bc || bc < rc) {
// cout << "Incorrect iterator return value when inserting " << *vit << endl;
// return -1;
// }
}
cout << " done." << endl;
// r->inorder();
//r->print_structure();
/* How many comparisons did it take to do the inserts, avg per key? */
double compsperkey = countint::getcount() / (double) N;
cout << "That took " << compsperkey << " average comparisons per key, " << endl;
cout << "Max compare key is: " << maxcompsperkey << endl;
if(compsperkey <= maxcompsperkey) cout << "OK. " << endl;
else if (compsperkey <= maxcompsperkey * 2) cout << "could be better... " << endl;
else {
cout << "way too many!" << endl;
return -1;
}
/* Test iterator; should iterate the entire tree inorder */
cout << "Checking traversal using iterator...";
vit = v.begin();
BST<countint>::iterator en = r->end();
BST<countint>::iterator it = r->begin();
int i = 0;
for(; it != en; ++it) {
// cout << *it << endl;
countint rc = *it;
countint bc = *vit;
if(rc < bc || bc < rc) {
cout << endl << "Incorrect inorder iteration of RST." << endl;
return -1;
}
++i;
++vit;
}
if(i!=N) {
cout << endl << "Early termination during inorder iteration of RST." << endl;
return -1;
}
cout << " OK." << endl;
return 0;
}