FAQ: Vectors - Adding and Removing Elements

This community-built FAQ covers the “Adding and Removing Elements” exercise from the lesson “Vectors”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Adding and Removing Elements

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi,
In this exercise, why is the vector declare as “std::string” instead of just “string”?
Thank you,
Megan

6 Likes

@ajaxcoder74881 hey megan, the C++ string class is part of the std namespace. so you simply have to include it :slight_smile:

2 Likes

great note tho. im going to add a hint in there just in case

1 Like

Why do we need to use .push_back and .pop_back?
I mean what is their function?

1 Like

to add/delete from a vector!

for example, suppose you are building a game and you want to keep track of an user’s friend or inventory list, you can create a vector that holds those people/items. and as they add a friend/item, you can use .push_back(). and as they delete a friend/item, you can use .pop_back().

2 Likes

Hi! How can we pop-back a specific index and/or a specific value?

Edit: For when you want to change just one value maybe in the middle of the vector, not every value as in the Operations lesson under Vectors.

But wouldn’t

std::vector [char] last_jedi;

also work?

square brackets above should be triangles but the forum somehow deletes what’s between them, sorry about that.

Since we haven’t discussed the char vs string difference in the previous lessons I’m confused what is the difference between char and string.

Hi!

Shall we add file #include string or it’s not necessary ?

What is a return value?

1 Like

please is there any particular way to know the reason and why there is a change in the letters color?

I tried .pop_back() function in the last_jedi case, I used this function right after the four strings “.push_back"ed, then cout from index 0 to 3. and I got the result :” kylo rey luke finn ".
Obviously the .pop_back() function didn’t work as predicted.
Then I tried to add another .push_back(“kylo”) after the .pop_back(), guess what I got ?
" kylo rey luke kylo "
So, it seems that .pop_back() don’t really remove the last element in the vector unless you add something to the vector after you .pop_back() it.
why? what if I really just want to remove it instead of replace it?

1 Like

pop_back() does remove the last element of the vector. But, when you are using square bracket notation to access elements of the vector, then you are responsible for not trying to use indices which may be out of range.

When you "push_back"ed four elements and then used pop_back to remove the last element, the vector now actually holds three elements.
std::cout << last_jedi[3] << " "; will still work even though last_jedi doesn’t have an element at index 3 any longer. Just the last data stored at that memory location has been shown as output. One of the perils of using square notation in C++ when you don’t make sure whether the index you are using is still valid.

Instead if you did something like std::cout << last_jedi.at(3) << " ";, you will see an out of range exception being thrown and the program will crash.

Have a look at “M M”'s answer in the thread https://stackoverflow.com/questions/32577576/when-using-vectors-does-pop-back-remove-values-along-with-elements

Have a look at the short paragraph titled “Array bounds checking” in http://www.dcs.bbk.ac.uk/~roger/cpp/week6.htm

2 Likes

Hi, the colours of the letters change to make it easy to identify the type of the string. In the downloadable console, all types are white.

Hi,

Can you tell more about why we should have to include namespace here? I thought we only need data type inside “<>”, which is only “string”? Why we add “std” in string type but not with others, like int or something?

Thanks for your help.

std::vectorstd::string dna = {“ATG”, “ACG”};
i didn’t get this one, why is it std::string and not only
it doesn’t even explain :confused:

Have a look at the earlier lesson (https://www.codecademy.com/courses/learn-c-plus-plus/lessons/cpp-vectors/exercises/creating-a-vector)
As it mentions, the syntax to create a vector in C++ is
std::vector<type> name;

In the example you mentioned, the elements of the vectors i.e. “ATG”, “ACG” are strings. So, the initialization is made via
std::vector<std::string> dna = {"ATG", "ACG"};

Hey,
Can we add element at a specific index number. Like we now that by using .pop_back method we can add elements in back of the vector but can we add a element at a specific index number.
For example: by doing this .pop_back(2);
2 is a index number.
Thanks you,
Saud.

pop_back removes the last element of the vector, while push_back adds an element to the end of the vector.

If you want to add element(s) at a certain index, you can use insert. You can remove element(s) at a certain index by using erase.

For specifics, see documentation:

There are a few variations in how the functions can be used. Consider the following:

std::vector<std::string> vec = {"a", "b", "c", "d"};
std::vector<std::string> vec2 = {"o", "p", "q"};

//---# Inserting an element at a certain index
//---# (Inserting "x" at index 1)
vec.insert(vec.begin() + 1, "x");
//---# vec is now {"a", "x", "b", "c", "d"}

//---# Inserting the same element multiple times
//---# (Inserting "y" four times at index 3)    
vec.insert(vec.begin() + 3, 4, "y");
//---# vec is now {"a", "x", "b", "y", "y", "y", "y", "c", "d"}

//---# Inserting another vector at a certain index
//---# (Inserting "p", "q" from vec2 at index 5 of vec)    
vec.insert(vec.begin() + 5, vec2.begin() + 1, vec2.end());
//---# vec is now {"a", "x", "b", "y", "y", "p", "q", "y", "y", "c", "d"}

//---# Removing element at a certain index
//---# (Removing element at index 2)    
vec.erase(vec.begin() + 2);
##--- vec is now {"a", "x", "y", "y", "p", "q", "y", "y", "c", "d"}


//---# Removing elements between two indexes
//--- #(Remove elements starting from index 1 and ending before 2nd last index)    
vec.erase(vec.begin() + 1, vec.end()- 2);
//---# vec is now {"a", "c", "d"}
1 Like

sometimes when you call a function it returns a value that’s it