Hello all,

I have been programing for a year or two and i dont get vectors. I have seen them with numbers but can someone give me an example of vector.erase with letters? like for an RPG: like sword, axe, armor. Then have them chose what they want to be deleted.

Thank you very much,
Gangsta gama

Hello all,

I have been programing for a year or two and i dont get vectors. I have seen them with numbers but can someone give me an example of vector.erase with letters? like for an RPG: like sword, axe, armor. Then have them chose what they want to be deleted.

Thank you very much,
Gangsta gama

Numbers, letters, it doesn't matter what the vector contains. An element is an element, no matter what its type is. Here's the example from this page, converted from integer to characters.

http://www.cplusplus.com/reference/stl/vector/erase/

// erasing from vector
#include <iostream>
#include <vector>
using namespace std;

int main ()
{
  unsigned int i;
  vector<char> myvector;

  // set some values (from 'A' to 'J')
  for (i=65; i<=74; i++) myvector.push_back(i);
  
  // erase the 6th element
  myvector.erase (myvector.begin()+5);

  // erase the first 3 elements:
  myvector.erase (myvector.begin(),myvector.begin()+3);

  cout << "myvector contains:";
  for (i=0; i<myvector.size(); i++)
    cout << " " << myvector[i];
  cout << endl;

  return 0;
}

I have read and ran the code u have said, but i have alot of questions :). One is: how did u get the letters there when u typed numbers? Second is what about if u wanted to put an item there? like for an RPG: u have in your inventory:

Rusty armor1
Rusty armor2
Rusty armor3
Rusty armor4
if someone wanted to say they wanted to delete rusty armor3 how could they do that?

Thank you again.

Same program using strings:

// erasing from vector
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main ()
{
  unsigned int i;
  vector<string> myvector;

  for (i=1; i< 10; i++)
  {
      char charNumber = '0' + i;
      string newElement = "Rusty Armor ";
      newElement.append (1, charNumber);
      myvector.push_back(newElement);
  } 
  
  // erase the 6th element
  myvector.erase (myvector.begin()+5);

  // erase the first 3 elements:
  myvector.erase (myvector.begin(),myvector.begin()+3);

  cout << "myvector contains:\n";
  for (i=0; i<myvector.size(); i++)
    cout << myvector[i] << endl;
  cout << endl;

  return 0;
}

Regarding assigning characters using numbers, see the ASCII table. 'A' is 65, 'J' is 74 in the ASCII table, so these two lines are the same:

char someCharacter = 65;
char someCharacter = 'a';

I use ASCII values in the program above too. '0' is 48 in ASCII. You can "add", so these lines are the same:

char someCharacter = '2';
char someCharacter = '0' + 2;
char someCharacter = 50;

Thank you for explaining the ASCII for me. I tryed to make a program. I get it better now thank you, just i still dont fully get it. I have made a program from your advice combined with a book I bought. It goes like this:

// Sample vector erase
#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main()
{
    unsigned int i;
    vector<string> myvector;
    int numItems = 0;
    const int MAX_ITEMS = 10;
    string inventory[MAX_ITEMS];
    
    inventory[numItems++] = "Battle Axe";
    inventory[numItems++] = "Armor";
    inventory[numItems++] = "Sword";
    
    cout << "Your inventory:" << endl;
    for (i=0; i < numItems; i++)
    cout << inventory[i] << endl;
    // This works perfectly
    
    
    system("PAUSE");
    return 0;
}

One problem is when i try to add the vector at the end, the program crashes. Please help me again :)

Thank you,
gangsta gama

Thank you for explaining the ASCII for me. I tryed to make a program. I get it better now thank you, just i still dont fully get it. I have made a program from your advice combined with a book I bought. It goes like this:

// Sample vector erase
#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main()
{
    unsigned int i;
    vector<string> myvector;
    int numItems = 0;
    const int MAX_ITEMS = 10;
    string inventory[MAX_ITEMS];
    
    inventory[numItems++] = "Battle Axe";
    inventory[numItems++] = "Armor";
    inventory[numItems++] = "Sword";
    
    cout << "Your inventory:" << endl;
    for (i=0; i < numItems; i++)
    cout << inventory[i] << endl;
    // This works perfectly
    
    
    system("PAUSE");
    return 0;
}

One problem is when i try to add the vector at the end, the program crashes. Please help me again :)

Thank you,
gangsta gama

I don't know how to help you because you posted code that you say works perfectly, but didn't post the code that crashes, so I have no idea what went wrong. You don't use a vector at all.

You're still using array, when you should be using vectors.
Here's how I'd do it:

void ShowInv(vector<string> inv){
    cout << "Your inventory:\n";
    for (unsigned int i = 0; i < inv.size(); i++)
        cout << inv.at(i) << endl;
}

int main()
{
    vector<string> inventory;

    inventory.push_back("Battle Axe");
    inventory.push_back("Armor");
    inventory.push_back("Sword");

    ShowInv(inventory);

    cin.get();
    return 0;
}

As you can see I've made a function to show your inventory. It's a bit cleaner looking. I've made a vector<string> to hold your items. Do you see how the show() function calls inv.size() to see how far it should loop?

Now if you're ready for some more advanced technique's, here the same code only now with a function added to delete items from your inventory. I'll let you stare at it for a bit :) If you have questions, don't hesitate to ask them:

#include<iostream>
#include<vector>
#include<string>

using namespace std;

void ShowInv(vector<string> inv){
    cout << "Your inventory:\n";
    for (unsigned int i = 0; i < inv.size(); i++)
        cout << inv.at(i) << endl;
}

void Pop(vector<string> * inv, string item){
    vector<string>::iterator  it;
    for (it = inv->begin(); it != inv->end(); ++it){
        if (*it == item){
            inv->erase(it);
            return;
        }
    }
    cout << "Item was not found!\n";
}

int main()
{
    vector<string> inventory;

    inventory.push_back("Battle Axe");
    inventory.push_back("Armor");
    inventory.push_back("Sword");

    ShowInv(inventory);
    Pop(&inventory, "Armor");
    ShowInv(inventory);

    cin.get();
    return 0;
}

good luck!

[edit]

Don't use system("pause"). use cin.get() instead.

Thank you niek_e but i have been away from programming for a while and i believe after this is all said and done you will get sick of me asking questions :).

Lets start with the simple small code:

First question: What is void ShowInv(vector<string> inv) mean? i know void ShowInv declares and names the function but what does the (vector<string> inv) mean? does it name another variable? if so what does vector<string> really mean?

Second question: what does for (unsigned int i = 0; i < inv.size(); i++) do? what number is inv.size()? and how do you get it? also for cout << inv.at(i) << endl; what is inv.at(i)?

Now i see the vector<string> again, what does that mean? it seems like int or double, but what does it do? also what does inventory.push_back mean? i know the syntax: (variable).push_back, but what does the push_back do?

and last ShowInv(inventory); calls the function, but why do you put inventory in parentheses?

You are probably thinking WOW when you see this. I know i have a lot of questions and im sorry for that. I have just been trying to figure this out for a long time. Thank you for taking the time to repsond.

Thank you again,
Gangsta gama

First, if you don't already have one, you need to get an online C++ guide that lists all libraries and all functions within those libraries. There are all sorts of vector tutorials out there as well, and always check Daniweb too. I personally like this site:

http://www.cplusplus.com

It has a section on vectors:

http://www.cplusplus.com/reference/stl/vector/

You'll notice it's part of stl (standard template library).

http://www.cplusplus.com/reference/stl/

Here's a key sentence:

A container is a holder object that stores a collection other objects (its elements). They are implemented as class templates, which allows a great flexibility in the types supported as elements.

That means it doesn't matter whether you are storing strings, ints, doubles, chars, or bananas. It all works the same and that's the whole idea. Key word is standard.

Within the vector page, scroll down to size and you'll see where niek_e's reference to the size () function comes in. It returns the number of elements in the vector. Same thing with at () . That function works like [] to access a particular element of the vector.

Also note the examples. What goes inside the the brackets is the type that is stored in the vector.

vector <string> inv
ShowInv(inventory);

This is like any other function call. The function takes a parameter of type vector <string> , as shown here in the declaration:

void ShowInv(vector<string> inv)

inventory is of type vector <string> and is being passed to the function, just like any other function parameter.

commented: Damn, you're fast :) +22
commented: explains very well. +2

Thank you niek_e but i have been away from programming for a while and i believe after this is all said and done you will get sick of me asking questions :).

You'll be amazed with my patience :)

First question: What is void ShowInv(vector<string> inv) mean? i know void ShowInv declares and names the function but what does the (vector<string> inv) mean? does it name another variable? if so what does vector<string> really mean?

vector<string> means that I'm declaring a vector of strings. So when I say void ShowInv(vector<string> inv) , I'm telling the compiler that I want a function that returns void, and I'm passing a variable of the type vector<string> (a vector of strings) which will be known by the name of "inv" in the scope of the function. Here's a tut on functions.

Second question: what does for (unsigned int i = 0; i < inv.size(); i++) do? what number is inv.size()? and how do you get it? also for cout << inv.at(i) << endl; what is inv.at(i)?

Let's start with the for-loop: for (unsigned int i = 0; i < inv.size(); i++) unsigned int i = 0 == declare i as an unsigned int and make it 0 i < inv.size() loop while i is smaller then the size of inv i++ Increment i each time we loop.

So essentially: loop through this piece of code while i is smaller then the size of inv, and increment i.

The vector has a function called size() which I use. What this function (in short) does is: It returns the number of elements that are stored in the vector. So in my example it returns 3, because we have 3 items in our inventory.

also what does inventory.push_back mean? i know the syntax: (variable).push_back, but what does the push_back do?

Let's imagine that we have a pile of books we want to put on a bookshelf. The books are the strings and the shelf is the vector.
Each time a push_back() is called, I'm putting one book on the shelf, starting left and moving my way to the right. The size() method returns the number of books on the shelf.

and last ShowInv(inventory); calls the function, but why do you put inventory in parentheses?

See my previous link on functions.

You are probably thinking WOW when you see this. I know i have a lot of questions and im sorry for that.

To be honest, it's nice to see something here that actually is trying to learn something instead of asking for complete codes for their homework assignment. If I thought you were that kind of poster, I wouldn't have replied at all ;)

[edit]
Damn, Vernon beat me to it. That's what I get for trying to post something with >10 words in it :)

commented: My condolences for being faster on the "post reply" button. Good post. +20
commented: Explains it very well. +2

Damn, Vernon beat me to it. That's what I get for trying to post something with >10 words in it :)

Yep, I saw you were viewing the page, so I posted, THEN edited my post. ;) +rep anyway for a good explanation!

Thank you both VERY much for replying to me. As i see you two are having a race to see who can post first. Please keep doing that, i need all the explanation i can get :). Okay, i now get mostly all of the small code that Niek_e posted except one thing, i still dont know what [icode]cout << inv.at(i) << endl;[/icode] means :). I get the variable: inv. I just dont get the at(i) part.

To kill two birds with one stone I am going to say what i dont get about the advanced code :). before i get to that i have a few other questions. What would you do if u wanted to add to the inventory again? would you [icode]inventory.push_back("ITEM");[/icode] again? and also what would you have to do if you asked the user what you wanted to be deleted? i heard there was vector.erase. i dont know how to use it though :).

Okay now the advanced code:

void Pop(vector<string> * inv, string item){
    vector<string>::iterator  it;
    for (it = inv->begin(); it != inv->end(); ++it){
        if (*it == item){
            inv->erase(it);

that TOTALY gets me lost. i think you are using pointers to point vector<string> to inv. and i have no clue what the ,string item means.

also dont know what [icode]vector<string>::iterator it[/icode] means. i was thinking about after an rpg word game i would move to 2d games but now i am having second thoughts :) i believe i need to master this first, am i correct?

next: it seems like u put[icode]cout << "Item was not found!\n";[/icode]in the middle of nowhere :) i dont get it.

and all seems the same from there on. except the [icode]Pop(&inventory, "Armor");[/icode] i dont get that either :)

Thank you very much,
Gangsta gama

i still dont know what cout << inv.at(i) << endl; means :). I get the variable: inv. I just dont get the at(i) part.

It's means the i-th object in the vector. In other words: If you say inv.at(i) you get the left most book on the shelf. inv.at(1) will get you the second from the left etc.

What would you do if u wanted to add to the inventory again? would you inventory.push_back("ITEM"); again?

correct!

and also what would you have to do if you asked the user what you wanted to be deleted? i heard there was vector.erase. i dont know how to use it though :).

That's what the pop() function does that I wrote. It takes 2 parameters: the first in the current vector of inventory, the second is the item that you want to remove as a string. If you run the "advanced" code you'll see that the first time I call show(), you'll see 3 items displayed. But the second time (after I Pop(&inventory, "Armor"); ) you'll have only 2 items left in your inventory.

Okay now the advanced code:
that TOTALY gets me lost. i think you are using pointers to point vector<string> to inv. and i have no clue what the ,string item means.

you're right on the pointers. The string item tells the compiler that this functions takes 2 parameters. The first is a vector<string> with your inventory in it. The second is a string which will be called 'item' within the scope of the function. This string is what the pop function will look for to delete from your inventory. As previously mentioned: run the code and compare the output before and after the pop-call.

also dont know what vector<string>::iterator it means.

I was afraid you'd ask that ;) . I think you can leave the pop-code as it is right now. If you gain more understanding on vectors and functions, this part will become clear to you.

i was thinking about after an rpg word game i would move to 2d games but now i am having second thoughts :) i believe i need to master this first, am i correct?

It's always a good idea to master the language basic functions first before you start trying to make a game. You might be disappointed if you don't. And remember: Rome wasn't build in a day!
next: it seems like u put cout << "Item was not found!\n"; in the middle of nowhere :) i dont get it.

except the Pop(&inventory, "Armor"); i dont get that either :)

Again: This will become clear when you learn how to use functions. Keyword: pass by reference.

For now all you need to know is that the function pop() takes 1 parameter which is your inventory-list (the vector<string>) and the second is the item you wish to remove from that inventory.

I'm signing of for now, I have other things to attend to. Keep working!

Thank you very much. I think all my questions are answered :). One more question: where do i go on from here? how do i get more understanding on the vectors and functions like the pop function?

Thank you very much!
Gangsta gama

Thank you very much. I think all my questions are answered :). One more question: where do i go on from here? how do i get more understanding on the vectors and functions like the pop function?

Thank you very much!
Gangsta gama

Go to cplusplus.com and see what functions are in the vector library. Write little sample programs using each of them, including iterators. If you don't know what an iterator, go to cplusplus.com and learn about THAT. If you don't know what "pop" refers to, learn about THAT. Look at all the stl options, not just vector, and read about when to use each. Learning takes you on several detours. Don't be afraid to take them, but remember to come back. Some well-phrased google queries will answer a lot of these questions. Note that vector has push_back and pop_back. With deque, you can pop and push to the front OR the back. That should give you an idea of what to use and when. niek_e wrote "pop" himself since vector doesn't have one. Experiment, google, then experiment some more!

Sweet thank you all. I will now make this thread solved :).

Thank youguys VERY much,
Gnagsta gama

Hmm, after reading my previous post I noticed 2 bugs. I said:

IIn other words: If you say inv.at(i) you get the left most book on the shelf. inv.at(1) will get you the second from the left etc.

But I meant to say:
If you say inv.[B]at(0)[/B] you get the left most book on the shelf.

Also it looks like I said this:

next: it seems like u put cout << "Item was not found!\n"; in the middle of nowhere :) i dont get it.

But this should've been a quote from you. I missed it completely.
The reason why that cout statement is in that place is that when an item was found in the pop-function, the function will erase it from the list and then call return , so it'll never reach that statement when an item is found. Only when an item is not found will it finish the loop and display the cout-statement.

Yep, I saw you were viewing the page, so I posted, THEN edited my post.

How could you see that then? niek_e uses invisible mode.

How could you see that then? niek_e uses invisible mode.

It's one of the perks of becoming a Posting Sensei. He's invisible to lesser mortals, but we get to see. ;)

It's one of the perks of becoming a Posting Sensei. He's invisible to lesser mortals, but we get to see. ;)

Hooray for hidden perks :) I'm not yet a posting sensei, but I have this shiny 'featured poster' badge which also has (or hasn't) it's perks ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.