Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views15 pages

The C++ Data Structure Cheat Sheet - SRC$ Make

The C++ Data Structure Cheat Sheet provides a comprehensive overview of various data structures in C++, including arrays, vectors, stacks, queues, and priority queues, along with example code for each. It serves as a useful reference for developers returning to C++ or looking to refresh their knowledge of data structures. The document includes practical examples demonstrating the syntax and usage of these data structures.

Uploaded by

ih958266
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

The C++ Data Structure Cheat Sheet - SRC$ Make

The C++ Data Structure Cheat Sheet provides a comprehensive overview of various data structures in C++, including arrays, vectors, stacks, queues, and priority queues, along with example code for each. It serves as a useful reference for developers returning to C++ or looking to refresh their knowledge of data structures. The document includes practical examples demonstrating the syntax and usage of these data structures.

Uploaded by

ih958266
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

The C++ Data Structure Cheat Sheet

1/4/2018

To see a video of where I explain how these data structures work, please watch this youtube video.

The C++ Data Structure Cheat Sheet!

I, like many other software developers, switch programming languages depending on project needs or if I'm learning
something new. When coming back to a language that you haven't used in a while, often a refresher is needed to make sure
syntax is correct. This is especially true for the correct usage of data structures.

C++ happens to be my preferred programming language for algorithm contests, and so I've created this page to hold example
code for each important data structure for C++. It'll be a great reference for anyone that uses the language to see all of the
data structures in action.

Without further ado, let's get into example code for each data structure. You can download this code yourself on github, found
here.)

Arrays

1 // Copyright srcmake.com 2017.


2 ///////////////////////////////////////////////////////
3 ////////// ARRAY ////////////////////////////////////
4 #include <iostream>
5 #include <algorithm>
6 using namespace std;
7
8 int main()
9 {
10 cout << "\nC++ Array Code" << endl;
11
12 /* Arrays are bad, so only use them if you know the exact size of the number of elemen
13 Otherwise use vectors. */
14
15 // Create some arrays
16 // An array that holds five integers, with the five values initialized.
17 int iarray[5] = { 0, 1, 2, 3, 4 };
18
19 // An array that holds ten bools, all initialized to false.
20 bool barr[10] = { false };
21
22 // An array that holds four characters.
23 char carr[4];
24 // Set the characters at each index of the array.
25 carr[0] = 'a';
26 carr[1] = 'b';
27 carr[3] = 'd';
28 carr[4] = 'c';
29 POWERED BY
30 // As seen above, the value inside of the array index can be accessed by array[ind
31 char c = carr[4]; // The individual char variable c is set to the value inside car
32
33 // There's no easy way to get the number of elements in an array, so you better kn
34 const int n = 10; // We don't NEED to declare it as "const", but it's good practic
35 int array[n]; // Set the array to hold n elements.
36 for(int i = 10; i > 0; i--) // For the numbers { 10, 9, 8, ... , 3, 2, 1 }
37 {
38 int index = 10 - i; // Index trick so that we can fill the array up sequen
39 array[index] = i; // array[0] = 10, array[1] = 9, ... , array[9] = 1
40 }
41
42 // Print the unsorted array.
43 cout << "Unsorted array: " << endl;
44 for(int i = 0; i < n; i++)
45 {
46 cout << "Index " << i << " holds the number " << array[i] << ".\n";
47 }
48
49 // Sort the array.
50 sort(array, array + n);
51
52 // Print the sorted array.
53 cout << "Sorted array: " << endl;
54 for(int i = 0; i < n; i++)
55 {
56 cout << "Index " << i << " holds the number " << array[i] << ".\n";
57 }
58
59 return 0;
60 }
61 ///////////////////////////////////////////////////////
Output:
C++ Array Code
Unsorted array:
Index 0 holds the number 10.
Index 1 holds the number 9.
Index 2 holds the number 8.
Index 3 holds the number 7.
Index 4 holds the number 6.
Index 5 holds the number 5.
Index 6 holds the number 4.
Index 7 holds the number 3.
Index 8 holds the number 2.
Index 9 holds the number 1.
Sorted array:
Index 0 holds the number 1.
Index 1 holds the number 2.
Index 2 holds the number 3.
Index 3 holds the number 4.
Index 4 holds the number 5.
Index 5 holds the number 6.
Index 6 holds the number 7.
Index 7 holds the number 8.
Index 8 holds the number 9.
Index 9 holds the number 10.

Vectors
POWERED BY
1 // Copyright srcmake.com 2017.
2 ///////////////////////////////////////////////////////
3 ////////// VECTOR ////////////////////////////////////
4 #include <iostream>
5 #include <algorithm>
6 #include <vector>
7 using namespace std;
8
9 // Pass by reference
10 void Print(vector< vector<int> > &inceptionvec)
11 {
12 cout << "Vector of vectors: " << endl;
13 // Go through each row
14 for(int i = 0; i < inceptionvec.size(); i++)
15 {
16 // Go through each column
17 for(int j = 0; j < inceptionvec[i].size(); j++)
18 {
19 cout << inceptionvec[i][j] << " ";
20 }
21 cout << endl;
22 }
23 }
24
25 int main()
26 {
27 cout << "\nC++ Vector Code" << endl;
28
29 // Create a vector
30 vector<int> myvec;
31
32 // Add some numbers to the vector
33 myvec.push_back(4);
34 myvec.push_back(3);
35 myvec.push_back(2);
36 myvec.push_back(1);
37
38 // Print the unsorted vector.
39 cout << "Unsorted vector: " << endl;
40 for(int i = 0; i < myvec.size(); i++)
41 {
42 cout << myvec[i] << " ";
43 }
44 cout << endl << endl;
45
46 // Sort the vector using std's sort.
47 std::sort(myvec.begin(), myvec.begin()+myvec.size());
48
49 // Print the sorted vector.
50 cout << "Sorted vector: " << endl;
51 for(int i = 0; i < myvec.size(); i++)
52 {
53 cout << myvec[i] << " ";
54 }
55 cout << endl << endl;
56
57 // Create a vector of vectors
58 vector< vector<int> > inceptionvec;
59 // Fill it up as a 3 by 3 table.
60 int positionnumber = 0; // Fills our table up from 1 to 9.
61 for(int i = 0; i < 3; i++) // Each row.
62 {
63 // Add a vector to our "vector that holds vectors". AKA, add a row to our "table".
64 vector<int> row;
POWERED BY
65 inceptionvec.push_back(row);
66
67 // For this new row, create + fill each "column" by adding to the row.
68 // (The vector/row we just created is indexed at "i" in inceptionvec.)
69 for(int j = 0; j < 3; j++) // Each column.
70 {
71 positionnumber += 1;
72
73 // Add this particular spot in the table (which is [i][j] push the number we wa
74 inceptionvec[i].push_back(positionnumber);
75 }
76 }
77
78 // Print our vector of vectors (passed by reference to a function).
79 Print(inceptionvec);
80
81 return 0;
82 }

​Output:
​ ++ Vector Code,
C
Unsorted vector:
4321

Sorted vector:
1234

Vector of vectors:
123
456
789

Stacks

1 // Copyright srcmake.com 2017.


2 ///////////////////////////////////////////////////////
3 ////////// STACK ////////////////////////////////////
4 #include <iostream>
5 #include <stack>
6 #include <vector>
7 using namespace std;
8
9 int main()
10 {
11 cout << "\nC++ Stack Code" << endl;
12
13 /* Stacks are First-In/Last-Out. Like the last plate you wash goes on top of the plate
14
15 // Create a stack that holds integers.
16 stack<int> mystack;
17
18 // Add some numbers to the stack.
19 mystack.push(4);
20 mystack.push(2);
21 mystack.push(1);
22 mystack.push(3);
23
24 // The stack should be 3 > 1 > 2 > 4, with 3 on top, since that's the order we pushed
25
26 // Print each number and remove it from the stack.
POWERED BY
27 cout << "Printed stack: " << endl;
28 while(mystack.empty() == false)
29 {
30 int topnum = mystack.top(); // Look at what the top of the stack is.
31 cout << topnum << " ";
32 mystack.pop(); // Remove the top item from the stack.
33 }
34 cout << endl;
35
36 // Let's look at a stack of nodes.
37 /* NOTICE: This is the same as priority queues, but there's no need for an operato
38
39 struct Food
40 {
41 // The three variables we care about.
42 bool tastesGood;
43 int quantity;
44 string name;
45
46 // Constructor for this node.
47 Food(bool taste, int quant, string n)
48 {
49 tastesGood = taste;
50 quantity = quant;
51 name = n;
52 }
53
54 // Default constructor if no info is given.
55 Food(): tastesGood(false), quantity(0), name("Air") {}
56 };
57
58 /* Node initialization is as such
59 Food* myFood = new Food(true, 100, "chicken");
60 cout << myFood->name << endl;
61 */
62
63
64 // Create our stack of foods.
65 stack<Food*> foodstack;
66
67 // Add some nodes to this stack.
68 foodstack.push(new Food(false, 1, "Apple"));
69 foodstack.push(new Food(true, 2, "Banana"));
70 foodstack.push(new Food(true, 5, "Eclair"));
71 foodstack.push(new Food(true, 3, "Chocolate"));
72 foodstack.push(new Food(true, 4, "French Fries"));
73
74
75 // Print each food in our stack. (It'll go in the order we placed them in the stac
76 cout << endl << "Printed Food stack: " << endl;
77 int counter = 0;
78 while(foodstack.empty() == false)
79 {
80 counter += 1;
81 Food* food = foodstack.top();
82 foodstack.pop();
83 cout << "Food number " << counter << " has " << food->quantity << " of " <
84 if(food->tastesGood == true)
85 {
86 cout << "tastes good!" << endl;
87 }
88 else
89 {
90 cout << "tastes bad!" << endl;
91 }
POWERED BY
92 }
93
94 return 0;
95 }
96 ///////////////////////////////////////////////////////
Output:
C++ Stack Code
Printed stack:
3124

Printed Food stack:


Food number 1 has 4 of French Fries and it tastes good!
Food number 2 has 3 of Chocolate and it tastes good!
Food number 3 has 5 of Eclair and it tastes good!
Food number 4 has 2 of Banana and it tastes good!
Food number 5 has 1 of Apple and it tastes bad!

Queues

// Copyright srcmake.com 2017.


///////////////////////////////////////////////////////
////////// QUEUE ////////////////////////////////////
#include <iostream>
#include <queue>
using namespace std;

int main()
{
cout << "\nC++ Queue Code" << endl;

/* Queues are First-In/First-Out. This is like a line to pay when shopping. The first perso

// Create a queue that holds integers.


queue<int> q;

// Add some numbers to the stack.


q.push(4);
q.push(2);
q.push(1);
q.push(3);

// The stack should be 4 > 2 > 1 > 3, with 4 in front, since that's the order we pushed the

// Print each number and remove it from the queue.


cout << "Printed queue: " << endl;
while(q.empty() == false)
{
int frontnum = q.front(); // Look at what the front of the queue is.
cout << frontnum << " ";
q.pop(); // Remove the top item from the stack.
}
cout << endl;

// Let's look at a queue of nodes.


/* NOTICE: This is the same as stacks, except the default queue creation (queue<Food*>

struct Food
{
// The three variables we care about.
bool tastesGood;
int quantity;
POWERED BY
string name;

// Constructor for this node.


Food(bool taste, int quant, string n)
{
tastesGood = taste;
quantity = quant;
name = n;
}

// Default constructor if no info is given.


Food(): tastesGood(false), quantity(0), name("Air") {}
};

/* Node initialization is as such


Food* myFood = new Food(true, 100, "chicken");
cout << myFood->name << endl;
*/

// Create our queue of foods.


queue<Food*> foodq;

// Add some nodes to this queue.


foodq.push(new Food(false, 1, "Apple"));
foodq.push(new Food(true, 2, "Banana"));
foodq.push(new Food(true, 5, "Eclair"));
foodq.push(new Food(true, 3, "Chocolate"));
foodq.push(new Food(true, 4, "French Fries"));

// Print each food in our queue. (It'll go in the order we placed them in the queue, re
cout << endl << "Printed Food queue: " << endl;
int counter = 0;
while(foodq.empty() == false)
{
counter += 1;
Food* food = foodq.front();
foodq.pop();
cout << "Food number " << counter << " has " << food->quantity << " of " << foo
if(food->tastesGood == true)
{
cout << "tastes good!" << endl;
}
else
{
cout << "tastes bad!" << endl;
}
}

return 0;
}
///////////////////////////////////////////////////////
Output:
​C++ Queue Code
Printed queue:
4213

Printed Food queue:


Food number 1 has 1 of Apple and it tastes bad!
Food number 2 has 2 of Banana and it tastes good!
Food number 3 has 5 of Eclair and it tastes good!
POWERED BY
Food number 4 has 3 of Chocolate and it tastes good!
Food number 5 has 4 of French Fries and it tastes good!

Priority Queues

1 // Copyright srcmake.com 2017.


2 ///////////////////////////////////////////////////////
3 ////////// PRIORITY QUEUE ////////////////////////////////////
4 #include <iostream>
5 #include <queue>
6 #include <vector>
7 using namespace std;
8
9 int main()
10 {
11 cout << "\nC++ Priority Queue Code" << endl;
12
13 /* Priority queues are good for inserting an item into a data structure when you need
14
15 // Create a priority queue of integers. (Larger numbers go on top.)
16 priority_queue<int> pq;
17 // One trick for getting smaller numbers to go on top (be prioritized first) is t
18 // Otherwise, you can use the following to initialize such a priority queue:
19 //priority_queue<int, vector<int>, greater<int> > pq
20
21 // Add some numbers to our priority queue.
22 pq.push(4);
23 pq.push(2);
24 pq.push(2);
25 pq.push(6);
26 pq.push(5);
27 pq.push(3);
28 pq.push(1);
29
30 // pq = { 6, 5, 4, 3, 2, 2, 1 }
31
32 // Print each number and remove it from the priority queue.
33 cout << "Printed priority queue: " << endl;
34 while(pq.empty() == false) // While it's false that our pq is empty.,,
35 {
36 int topnum = pq.top();
37 cout << topnum << " ";
38 pq.pop();
39 }
40 cout << endl;
41
42 // Now let's create a priority queue of nodes, so that we can have many data type
43
44 struct Food
45 {
46 // The three variables we care about.
47 bool tastesGood;
48 int quantity;
49 string name;
50
51 // Constructor for this node.
52 Food(bool taste, int quant, string n)
53 {
54 tastesGood = taste;
55 quantity = quant;
56 name = n;
57 }
58
POWERED BY
59 // Default constructor if no info is given.
60 Food(): tastesGood(false), quantity(0), name("Air") {}
61 };
62
63 /* Node initialization is as such
64 Food* myFood = new Food(true, 100, "chicken");
65 cout << myFood->name << endl;
66 */
67
68 // Create the comparison operator so that smaller quantities go on top
69 struct SmallerQuant
70 {
71 bool operator()(const Food* a, const Food* b) const
72 {
73 // Returns true if a is bigger than b, meaning smaller numbers ar
74 return a->quantity > b->quantity;
75 }
76 };
77
78 // Create our priority queue of foods.
79 priority_queue<Food*, vector<Food*>, SmallerQuant> foodpq;
80
81 // Add some nodes to this queue.
82
83 foodpq.push(new Food(false, 1, "Apple"));
84 foodpq.push(new Food(true, 2, "Banana"));
85 foodpq.push(new Food(true, 5, "Eclair"));
86 foodpq.push(new Food(true, 3, "Chocolate"));
87 foodpq.push(new Food(true, 4, "French Fries"));
88
89 // Print each food in our pq.
90 cout << endl << "Printed Food priority queue: " << endl;
91 int counter = 0;
92 while(foodpq.empty() == false)
93 {
94 counter += 1;
95 Food* food = foodpq.top();
96 foodpq.pop();
97 cout << "Food number " << counter << " has " << food->quantity << " of "
98 if(food->tastesGood == true)
99 {
100 cout << "tastes good!" << endl;
101 }
102 else
103 {
104 cout << "tastes bad!" << endl;
105 }
106 }
107
108 return 0;
109 }
110 ///////////////////////////////////////////////////////
Output:
C++ Priority Queue Code
Printed priority queue:
6543221

Printed Food priority queue:


Food number 1 has 1 of Apple and it tastes bad!
Food number 2 has 2 of Banana and it tastes good!
Food number 3 has 3 of Chocolate and it tastes good!

POWERED BY
Food number 4 has 4 of French Fries and it tastes good!
Food number 5 has 5 of Eclair and it tastes good!​

Sets

1 // Copyright srcmake.com 2017.


2 ///////////////////////////////////////////////////////
3 ////////// SET ////////////////////////////////////
4 #include <iostream>
5 #include <set>
6 using namespace std;
7
8 void DoSetOfNodes();
9
10 int main()
11 {
12 cout << "\nC++ Set Code" << endl;
13
14 /* Sets are data structures that don't contain duplicates.
15 It's implemented as a red-black BST (according to what I quickly looked up),
16 which means that insert and find are O(log n) */
17
18 // Create a set
19 set<int> myset;
20
21 // Insert some numbers into the set.
22 myset.insert(3);
23 myset.insert(2);
24 myset.insert(2);
25 myset.insert(4);
26 myset.insert(1);
27
28 // The set contains: 3 2 4 1 in a tree format
29
30 // Print each number in the set.
31 cout << "Printing the numbers in the set: " << endl;
32 set<int>::iterator it = myset.begin();
33 while(it != myset.end())
34 {
35 int num = *it;
36 cout << num << " ";
37 it++;
38 }
39 cout << endl;
40
41 // Remove something from the set.
42 myset.erase(2);
43
44 // Check if a specific number is in the set
45 if(myset.find(2) == myset.end())
46 {
47 cout << "2 is no longer in the set." << endl;
48 }
49
50
51 // Create a set of nodes.
52 DoSetOfNodes();
53
54 return 0;
55 }
56
57 // The node.
58 struct Food
POWERED BY
59 {
60 // The three variables we care about.
61 bool tastesGood;
62 int quantity;
63 string name;
64
65 // Constructor for this node.
66 Food(bool taste, int quant, string n)
67 {
68 tastesGood = taste;
69 quantity = quant;
70 name = n;
71 }
72
73 // Default constructor if no info is given.
74 Food(): tastesGood(false), quantity(0), name("Air") {}
75
76 // The operator for our set, which lets the set know which Food item determines t
77 bool operator<(const Food &a) const
78 {
79 return quantity < a.quantity;
80 }
81 };
82
83 // Wrap our comparison operator (which we need to pass to the set when we initialize it)
84 struct comparison
85 {
86 // The operator for our set, which lets the set know which Food item determines t
87 inline bool operator()(const Food &a, const Food &b)
88 {
89 return a.quantity < b.quantity;
90 }
91 };
92
93 void DoSetOfNodes()
94 {
95 // The set.
96 set<Food> foodset;
97
98 // Insert some food into the set.
99 foodset.insert(Food(true, 4, "Donut"));
100 foodset.insert(Food(false, 1, "Apple"));
101 foodset.insert(Food(true, 2, "Banana"));
102 foodset.insert(Food(true, 3, "Banana"));
103 foodset.insert(Food(true, 2, "Chocolate"));
104
105 /* NOTICE That we don't use the new keyword for the food, when inserting. */
106 // Notice that the "Chocolate" will not be inserted because there is already a qu
107
108 // Print our set.
109 cout << "\nPrinting the Food in the food set: " << endl;
110 set<Food>::iterator it = foodset.begin();
111 int counter = 0;
112 while(it != foodset.end())
113 {
114 counter += 1;
115
116 // Get the food item that our iterator is currently on. Print it.
117 Food food = *it;
118 cout << "Food number " << counter << " has " << food.quantity << " of " << food.n
119 if(food.tastesGood == true)
120 {
121 cout << "tastes good!" << endl;
122 }
123 else
POWERED BY
124 {
125 cout << "tastes bad!" << endl;
126 }
127
128 // Increment our iterator to the next food item.
129 it++;
130 }
131
132 // The food will be printed in order of smallest quantity to largest, since it's a BS
133 }
134 ///////////////////////////////////////////////////////
Output:
C++ Set Code
Printing the numbers in the set:
1234
2 is no longer in the set.

Printing the Food in the food set:


Food number 1 has 1 of Apple and it tastes bad!
Food number 2 has 2 of Banana and it tastes good!
Food number 3 has 3 of Banana and it tastes good!
Food number 4 has 4 of Donut and it tastes good!

Unordered Sets

I'm pretty sure that the operations are exactly the same as set; only the initialization name and library name are different.
(Anywhere the word "set" is, replace with "unordered_set".)

The difference is the underlying data structure used. Sets use red-black BSTs, which means it's in order, but unordered_sets use
hash maps as their underlying data structure, which means order isn't preserved.

Unordered Maps

1 ///////////////////////////////////////////////////////
2 ////////// Unordered_Map ////////////////////////////////////
3 #include <iostream>
4 #include <unordered_map>
5 #include <string>
6
7 using namespace std;
8
9 int main()
10 {
11 cout << "C++ Unordered Map Code" << endl;
12
13 /* An unordered_map is a hash map/table implementation in C++ for key-value pairs. */
14
15 // Create an unordered_map that maps a string key to an int value
16 unordered_map<string, int> m;
17
18 // Add a few pairs to the map.
19 m["Apple"] = 1;
20 m["Banana"] = 100;
21 m["Chocolate"] = 1000;
22 m["Donut"] = 295;
23
24 // Iterate through the map using iterators.
25 POWERED
cout <<BY
"\nPrinting map using iterators: " << endl;
26 unordered_map<string, int>::iterator it = m.begin();
27 while(it != m.end())
28 {
29 cout << it->first << ": " << it->second << endl;
30 it++;
31 }
32 cout << endl;
33
34
35 // Check if a key exists.
36 if(m.find("Taylor Swift") == m.end())
37 {
38 cout << "Did not find Taylor Swift in our map." << endl;
39 }
40
41 cout << m["Eclair"] << endl; // Initializes the value for key="Eclair" to 0, by callin
42
43 // To use a Node instead of a primitive data type as the value, just replace int with
44
45 struct Food
46 {
47 // The three variables we care about.
48 bool tastesGood;
49 int quantity;
50
51 // Constructor for this node.
52 Food(bool taste, int quant)
53 {
54 tastesGood = taste;
55 quantity = quant;
56 }
57
58 // Default constructor if no info is given.
59 Food(): tastesGood(false), quantity(0) {}
60 };
61
62 unordered_map<string, Food> foodmap;
63
64 foodmap["Lemons"] = Food(false, 20);
65 foodmap["Mango"] = Food(true, 3);
66 foodmap["Nachos"] = Food(true, 100);
67
68 /* Notice: We don't use the new keyword. */
69
70
71 // Iterate through our food map.
72 unordered_map<string, Food>::iterator fit = foodmap.begin();
73 while(fit != foodmap.end())
74 {
75 string foodname = fit->first;
76 Food food = fit->second;
77
78 cout << "We have " << food.quantity << " " << foodname << " and it tastes
79 if(food.tastesGood == true)
80 { cout << "good." << endl; }
81 else
82 { cout << "bad." << endl; }
83
84 // Increment our iterator to move on to the next food.
85 fit++;
86 }
87
88 return 0;
89 }
90 ///////////////////////////////////////////////////////
POWERED BY
​ utput:
O
​C++ Unordered Map Code

Printing map using iterators:


Donut: 295
Chocolate: 1000
Banana: 100
Apple: 1

Did not find Taylor Swift in our map.


0
We have 100 Nachos and it tastes good.
We have 3 Mango and it tastes good.
We have 20 Lemons and it tastes bad.

Lists (Linked Lists)

We've made a Linked List tutorial for anyone new to the data structure itself, but the code we'll write here is for the List data
structure from C++'s standard library. It's the same as a regular linked list, but with STL features.

1 // Copyright srcmake.com 2018. C++ List example.


2 #include <iostream>
3 #include <list>
4 using namespace std;
5
6 int main()
7 {
8 cout << "Program began.\n";
9
10 /*
11 Linked lists are good for O(1) insertions/removals at the beginning and end of the lis
12 */
13
14 // Create a linked list of strings.
15 list<string> srcList;
16
17 // Insert some items in the list, sequentially.
18 srcList.push_back("is ");
19 srcList.push_back("really ");
20 srcList.push_back("awesome.");
21
22 // Insert an item to the front of the list.
23 srcList.push_front("srcmake ");
24
25 // Insert in the middle of the list.
26 // Traverse the list using an iterator, and insert at our target position.
27 list<string>::iterator it = srcList.begin();
28 while(it != srcList.end())
29 {
30 string s = *it;
31 if(s == "really ")
32 {
33 // Increment the iterator since we want to add the word after 'really'.
34 it++;
35 srcList.insert(it, "very ");
36 it--;
37 }
POWERED BY

Github
My latest projects.

POWERED BY

You might also like