Thread: adding multiple things to one vector

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    12

    adding multiple things to one vector

    Hi,

    I'm trying to include multiple things into a vector. My program is suppose to read from an input file that looks something like

    add 12345 12.50 50

    add - tells me I need to add this to a list, which I'll be using vector
    12345 - that's the id number
    12.50 - the price
    50 - quantity

    I'm going to need to use a linear search later if the input file tells me to change the price or quantity but I'm stuck trying to how to add them in the first place. I'm also trying to do this using a class.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Forget about writing code for while and try to work it out on paper.

    Start with defining a class using three member variables, one for id number, on for the price and one for the quantity.

    Then look up how to use the std::vector class to add items to a vector.

    In theory you if the first token in the line is add you will add a new object to the vector if it's id doesn't already exist in the vector and increase the quantity if it does already exist. Similarly, if the first token is remove then you will decrease the quantity or add it to the vector and put in a back order if it doesn't already exist.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Oh... I'm doing that as I go along, writing it down and then putting the code in.

    Does anyone know where I can find a sample where it uses vectors and class in one program?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is really rather easy. vector<MyClass> myVec; myVec.push_back(myClassVar); etc. If you know how to use vector with an int it is not much different with a class.

    Did you have any specific problems when you attempted to do it?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's some information on vectors. It has all you need on how to add an element to a vector of elements.

    http://www.cppreference.com/cppvector/index.html

    the std::vector class is templated so all you have to do is put the name of your class in the angle brackets instead of int when you declare the vector:

    //declare a vector that will hold elements of type int
    std::vector <int> myInts;

    just be sure to add the vector header file and use either the std:: prefix before each declaration of a std::vector object or type an appropriate using statement before main() to gain access to the file contents.
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Thank you both elad and Daved soooooooo much. At first, when I came to this board, I'm like "oh I'm probably not going to get any help but it's worth a shot," but that's going to help me a lot. And you guys explained it so much better than my professor did...

    Can my vector have the follow elements: a string, an int, and a double all in one... vector?
    Last edited by brooklyn; 04-14-2006 at 12:04 PM.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by brooklyn
    Can my vector have the follow elements: a string, an int, and a double all in one... vector?
    No. When you declare a vector you have to specify the type of elements it holds.
    The closest you can get to insert different types of objects is to declare it to hold pointers to a common baseclass.
    Kurt

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Can my vector have the follow elements: a string, an int, and a double all in one... vector?

    Yes, but that uses some advanced stuff that you don't need or want (e.g. boost::variant). Based on the description of your problem it makes more sense to combine the data in a class and store instances of the class in your vector.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    So I can't do like

    Header file:
    Code:
    public:
    	inputnewitemd(string n, int q, double c)	
    		:itemNumber(n), quantity(q), cost(c);
    cpp file of the class:
    Code:
    inventoryItem::inputnewitemD( string n, int q, double c )
    {
    	vector < inventoryItem > inventoryList; //vector to store the inventory item
    	inputnewItem a( n, q, c );
    	inventoryList.push_back(a);	//adds inventory number to vector
    }
    because the program will have to change either q and/or c of n after n is added into a vector (q and c of that n will have to be added in too). I'll have to search for n and then change that specific element's q and/or c.

    Last edited by brooklyn; 04-14-2006 at 12:31 PM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    is this what you are trying to do ?
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class item {
    
    public:
       item( int i_, string s_ ):i(i_), s(s_) {}
       
       void set_i(int i_)  { i = i_; }
       void set_s(string s_)  { s = s_; }
       
       int get_i()  { return i; }
       string get_s()  { return s; }
    private:       
       int i;
       string s;        
    };
    
    
    int main() {
        
        vector<item> vec;
        
        vec.push_back( item( 1, "one" ) );
        vec.push_back( item( 2, "two" ) );
    
        for ( int i = 0; i < vec.size() ; ++i )
            if ( vec[i].get_i() == 2 ) 
                vec[i].set_s("zwei");
                        
        for ( int i = 0; i < vec.size() ; ++i )
            cout << vec[i].get_i() << " " << vec[i].get_s() << endl;
                        
        return 0;
    }
    Kurt

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    wow... that is...

    Thanks

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your code idea was fine. You have an inventoryItem class that holds the itemNumber, quantity and price. You create a new instance and pass it values for those properties, and then you push that instance on to the vector. The only problems in your posted code are with naming. For some reason you are using inputnewItem instead of inventoryItem as the type of your variable called a, and you used inputnewitemd instead of inventoryItem as the name of your constructor for the inventoryItem class. Fix those and you are on the right track.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Thanks again.

    I understood what Zuk did by finding item 2 and replacing zwei with two:

    Quote Originally Posted by ZuK
    is this what you are trying to do ?
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class item {
    
    public:
       item( int i_, string s_ ):i(i_), s(s_) {}
       
       void set_i(int i_)  { i = i_; }
       void set_s(string s_)  { s = s_; }
       
       int get_i()  { return i; }
       string get_s()  { return s; }
    private:       
       int i;
       string s;        
    };
    
    
    int main() {
        
        vector<item> vec;
        
        vec.push_back( item( 1, "one" ) );
        vec.push_back( item( 2, "two" ) );
    
        for ( int i = 0; i < vec.size() ; ++i )
            if ( vec[i].get_i() == 2 ) 
                vec[i].set_s("zwei");
                        
        for ( int i = 0; i < vec.size() ; ++i )
            cout << vec[i].get_i() << " " << vec[i].get_s() << endl;
                        
        return 0;
    }
    Kurt
    I see what you did there. Very nice. But what if "one" and "two" were integers say:

    item 1 has 3 as the integer
    item 2 has 4 as an interger

    I want to find item 2 and add whatever the input file reads to whatever integer item 2 has.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class item {
    
        public:
            item( int id_, int qt_, float pr_ ):id(id_), qt(qt_), pr(pr_) {}
       
            void set_id(int id_)    { id = id_; }
            void set_qt(int qt_)    { qt = qt_; }
            void set_pr(float pr_)  { pr = pr_; }
       
            int get_id()    { return id; }
            int get_qt()    { return qt; }
            float get_pr()  { return pr; }
        private:       
            int id;
            int qt;        
            float pr;        
    };
    
    
    int main() {
        
        vector<item> vec;
        
        vec.push_back( item( 1, 5, 12.5 ) );
        vec.push_back( item( 5, 9, 10.5 ) );
        vec.push_back( item( 4, 8, 4.5 ) );
        vec.push_back( item( 9, 3, 7.7 ) );
    
        float to_add = 15.33;
        int item_to_change = 4;
    
        cout << "before changing item " << item_to_change << endl;
        for ( int i = 0; i < vec.size() ; ++i )
            cout << vec[i].get_id() << ": " << vec[i].get_qt() << " " << vec[i].get_pr() << endl;
        
        // search for item with id 'item_to_change'    
        for ( int i = 0; i < vec.size() ; ++i )
            if ( vec[i].get_id() == item_to_change )
                // add 'to_add' to the price 
                vec[i].set_pr( vec[i].get_pr() + to_add );
                        
        cout << "after changing item " << item_to_change << endl;
        for ( int i = 0; i < vec.size() ; ++i )
            cout << vec[i].get_id() << ": " << vec[i].get_qt() << " " << vec[i].get_pr() << endl;
                        
        return 0;
    }
    Kurt

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Quote Originally Posted by ZuK
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class item {
    
        public:
            item( int id_, int qt_, float pr_ ):id(id_), qt(qt_), pr(pr_) {}
       
            void set_id(int id_)    { id = id_; }
            void set_qt(int qt_)    { qt = qt_; }
            void set_pr(float pr_)  { pr = pr_; }
       
            int get_id()    { return id; }
            int get_qt()    { return qt; }
            float get_pr()  { return pr; }
        private:       
            int id;
            int qt;        
            float pr;        
    };
    
    
    int main() {
        
        vector<item> vec;
        
        vec.push_back( item( 1, 5, 12.5 ) );
        vec.push_back( item( 5, 9, 10.5 ) );
        vec.push_back( item( 4, 8, 4.5 ) );
        vec.push_back( item( 9, 3, 7.7 ) );
    
        float to_add = 15.33;
        int item_to_change = 4;
    
        cout << "before changing item " << item_to_change << endl;
        for ( int i = 0; i < vec.size() ; ++i )
            cout << vec[i].get_id() << ": " << vec[i].get_qt() << " " << vec[i].get_pr() << endl;
        
        // search for item with id 'item_to_change'    
        for ( int i = 0; i < vec.size() ; ++i )
            if ( vec[i].get_id() == item_to_change )
                // add 'to_add' to the price 
                vec[i].set_pr( vec[i].get_pr() + to_add );
                        
        cout << "after changing item " << item_to_change << endl;
        for ( int i = 0; i < vec.size() ; ++i )
            cout << vec[i].get_id() << ": " << vec[i].get_qt() << " " << vec[i].get_pr() << endl;
                        
        return 0;
    }
    Kurt

    That shows almost exactly what I have to do. Except that in that program
    float to_add = 15.33;
    int item_to_change = 4;

    they're declared. I have to read them off the input file which the "to_add" variable is the same as the variable that I read it. I figure I should implement some swapping, but it doesn't look right. This is what I have:

    Code:
    //this function will change the quantity number of the product
    int inventoryItem::getQuantity( string n, int q, double c )
    {
    	string getQuantity(vector <inventoryItem> &inventoryList, string n);
    	string i = 0;
    
    			int q_q = 0;
    			void swap (int q_q, int q);
    			//swaps the searched quantity into q_q
    			int c = q_q;
    			q_q = q;
    			q = c;
    
    	for ( i = 0; i < inventoryList.size(); i++)
    	{
    		if (inventoryList[i].getQuantity == n) //searchs for the item number 
     //that the input file has just been read
    		{
    			
    			inventoryList[i].setQuantity( inventoryList[i].getQuantity() + q_q);	
    			iventoryList.push_back( n, q, c );	//stores the new quantity along with
    							//itemNumber and cost into vector
    
    			cout << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;
    			Output << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;		
    		}
    		else
    		{
    			Error <<"Error: " << n << "not in inventory"<<endl;
    		}
    	}
    	return -1;
    }
    Sorry, I'm an extreme newbie at C++.
    Last edited by brooklyn; 04-14-2006 at 03:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  3. stop a recursion
    By acosgaya in forum C++ Programming
    Replies: 5
    Last Post: 10-31-2006, 10:46 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. My 3D Vector class
    By Lurker in forum C++ Programming
    Replies: 105
    Last Post: 11-16-2003, 05:58 PM