本文整理汇总了C++中Bag::add方法的典型用法代码示例。如果您正苦于以下问题:C++ Bag::add方法的具体用法?C++ Bag::add怎么用?C++ Bag::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bag
的用法示例。
在下文中一共展示了Bag::add方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
Bag<int> mBag;
cout << "size: " << mBag.getSize() << endl;
cout << "isEmpty(): " << mBag.isEmpty() << endl;
cout << "adding 1,2,3...\n";
mBag.add(1);
mBag.add(2);
mBag.add(3);
cout << "size: " << mBag.getSize() << endl;
cout << "isEmpty(): " << mBag.isEmpty() << endl << endl;
cout << "Contents: " << mBag;
Bag<int> mBag2;
cout << "\nTesting equal operator: ";
mBag2 = mBag;
cout << mBag2 << endl;
cout << "Testing copy constructor: ";
Bag<int> mBag3(mBag2);
cout << mBag3 << endl;
cout << "\nTesting remove(2): ";
mBag3.remove(2);
cout << mBag3 << endl;
cout << "\nTesting clear(): ";
mBag3.clear();
cout << mBag3 << endl;
cout << "\nTesting contains(3): " << mBag2.contains(3) << endl;
mBag2.contains(3);
cout << mBag2 << endl;
cout << "\nTesting contains(7): " << mBag2.contains(7) << endl;
mBag2.contains(7);
cout << mBag2 << endl;
Bag<int> mBag4;
mBag4.add(1);
mBag4.add(2);
mBag4.add(3);
mBag4.add(4);
mBag4.add(2);
cout << "\nTesting getFrequency(2): " << mBag4.getFrequency(2) << endl;
cout << "Bag contains: " << mBag4 << endl;
Bag<int> test;
cout << "\nTesting == operator test==bag4: " << (test == mBag4) << endl;
cout << "Testing != operator test!=bag4: " << (test != mBag4) << endl;
test += 1;
cout << "Testing +=: contents " << test << endl;
test += 33;
cout << "Contents: " << test <<endl;
cout << "Testing -=: contents " << test << endl;
test -= 33;
cout << "Contents: " << test <<endl;
return 0;
}
示例2: main
int main() {
Bag<int> xi;
Bag<char> xc;
Bag<int*> xp; // Uses partial specialization for pointer types.
xi.add(10);
xi.add(9);
xi.add(8);
xi.print();
xc.add('a');
xc.add('b');
xc.add('c');
xc.print();
int i = 3, j = 87, *p = new int[2];
*p = 8;
*(p + 1) = 100;
xp.add(&i);
xp.add(&j);
xp.add(p);
xp.add(p + 1);
p = NULL;
xp.add(p);
xp.print();
}
示例3:
Digraph& Digraph::operator=(const Digraph& G) {
printf("Assigning Digraph\n");
if (this == &G) return *this;
// Free memory
delete[] adj_;
// Allocate memory
V_ = G.V_;
E_ = G.E_;
Bag<int>* new_adj = new Bag<int>[G.V_];
// Copy elements
for (int v = 0; v < G.V_; v++) {
// reverse so that adjacency list is in the same order as original
Bag<int> reverse;
for (int w : G.adj_[v])
reverse.add(w);
for (int w : reverse)
new_adj[v].add(w);
}
// Reassign variables
adj_ = new_adj;
return *this;
}
示例4: main
int main() {
Bag<int> lengths;
Bag<char, 26> letters;
Bag<string, 20000> words;
ifstream is("/Users/alexandernohe/Downloads/labs_icpp/Exercises/Skeletons/Tale.txt");
if(!is)
{
cerr << "Cannot open Tale.txt!" << endl;
return 1;
}
string s;
while (is >> s)
{
string w;
for (int i=0; i < s.length(); i++)
{
char ch = s[i];
if (isalpha(ch))
{
w += toupper(ch);
letters.add(toupper(ch));
}
}
lengths.add(int(w.length()));
words.add(w);
}
cout << "-- lengths --" << endl;
cout << "1:" << lengths.count(1) << " 2:" << lengths.count(2) << " 3:" << lengths.count(3) << " 4:" << lengths.count(4) << " 5:" << lengths.count(5) << std::endl;
std::cout << "-- letters --" << std::endl;
cout << "A:" << letters.count('A') << " B:" << letters.count('B') << " C:" << letters.count('C') << endl;
std::cout << words.uniqueSize() << " out of " << words.count() << " are unique " << (double((words.uniqueSize())/double(words.count()))*100) << "%" << endl;
for (int i=0; i < words.count(); i++) {
std::string w;
int n = words.item(i, w);
if(n != 0)
{
cout << n << ":" << w << "\n";
}
}
is.close();
}