As part of learning to use the Standard Template list, I wrote a simple program.
The idea behind the program is to take 'n' number of words and then arrange them in a order.
Instead of the using the default "Sort" operation. I'm trying to arrange the words in a sequence as the user enters the name.
For example.
If the user specifies he wants to enter 4 names.
He starts with name 1: Hillary
Name 2: Donald
After you enter name 2 . it will show you the sorted list so far. which is
Donald
Hillary
Then the user is prompted for name 3.
Name 3: Pussygrabber
At this time, the list is further sorted and the new list looks like,
Donald
Hillary
Pussygrabber
The user is prompted for name 4:
Name 4: Warcriminal
Now before it starts the sorting operation, it throws up an error
" StandardTemplatelibrary(1550,0x1001243c0) malloc: *** mach_vm_map(size=140734799806464) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc"
Can't figure what I have done wrong in the program. Hoping someone can point out something silly that I must have missed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <list>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
// list<int> list_of_ints;
// list<double> list_of_doubles;
list<string> list_of_strings;
int n;
string s0, s1, s2;
char t1, t2;
cout << "Enter the number of strings you would like to store: " ;
cin >> n;
list<string>::iterator iter = list_of_strings.begin();
for (int i = 0; i < n; i++) {
cout << "Enter name " << i + 1 << " :" ;
cin >> s1;
if (i == 0 ) {
s0 = *iter ;
list_of_strings.insert(iter, s1);
iter++;
}
else{
*iter += s0;
t1 = s1[0];
for (int j = 0; j< i; j++) {
s2 = *iter ;
t2 = s2[0];
if (t1 < t2) {
break;
} else {
iter++;
}
}
t2 = s2[0];
if (t1 < t2) {
list_of_strings.insert(iter, s1);
iter = list_of_strings.begin();
while (iter != list_of_strings.end()) {
cout << *iter << endl;
iter++;
}
} else {
list_of_strings.push_back(s1);
iter = list_of_strings.begin();
while (iter != list_of_strings.end()) {
cout << *iter << endl;
iter++;
}
}
}
}
// list_of_strings.sort();
return 0;
}
|