Thread: Vector checking

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Vector checking

    I was wondering if there was a way to check if theres already stored in a vector element.
    For example:
    Code:
    vector<string> Inventory;
    I searched up vector methods and I saw the empty() method but, then it says it checks if the whole vector is empty. Could i do something like this?

    Code:
    Inventory[0].empty();

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    no you can't. Inventory[0] in this case is of type "string".

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    70
    Is there any way I'm able to do this then?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    if the 0th element has not been written yet, it will be the value created by the default constructor of "string" class.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    what do you mean by "there's already stored in a vector element", are you trying to check if the 0th string is written? Then you can compare the 0th element with a string created by the default constructor of "string".

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    70
    Like I'm trying to write a method of class Human thats named getItem. It is suppose to check if the 0th element of the vector is empty or not written yet and store the string(Name of item) in there but if it is empty i want to skip to the next element and check if its written or not again.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    Oh in that case, "Inventory[0].empty();" actually does work, since the class "string" has this method too, and since the default strings are empty, if it's empty, it means it's not written yet.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    70
    Okay thanks

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can check the vector's size. If the size is n, then there are n elements stored from [0, n-1].
    Other than that, you can also try using the at method. If the index you are trying to access doesn't exist, it will throw an exception.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It sounds like you could make your life easier by using something like this.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>  // for std::find
    
    class Data {
    private:
        std::vector<std::string> data;
    public:
        void add(std::string s);
        bool contains(std::string s);
    };
    
    void Data::add(std::string s) {
        data.push_back(s);
    }
    
    bool Data::contains(std::string s) {
        std::vector<std::string>::iterator found
            = std::find(data.begin(), data.end(), s);
        
        // If s does not exist within the range [begin(), end()) then
        // end() will be returned, meaning that false should be returned
        // from this function. Likewise, if s does exist then some value
        // other than end() will be stored in found and true is thus
        // returned.
        
        return found != data.end();
    }
    
    int main() {
        Data data;
        
        data.add("one");
        data.add("two");
        
        if(data.contains("two")) std::cout << "contains \"two\"\n";
        else std::cout << "does not contain \"two\"\n";
        
        if(data.contains("four")) std::cout << "contains \"four\"\n";
        else std::cout << "does not contain \"four\"\n";
        
        return 0;
    }
    The output is
    Code:
    contains "two"
    does not contain "four"
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'd rather use a map. Searching a vector is a O(n) operation.
    Unless it's sorted, which would mean you could use a binary search... but I don't think there IS such a search in the standard library.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The primary part of dwks' suggestion is really about using the std::find generic algorithm. You do not have to define another class, though you could define a convenience function, e.g.,
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>  // for std::find
    
    bool contains(const std::vector<std::string>& data, const std::string& s) {
        return std::find(data.begin(), data.end(), s) != data.end();
    }
    
    int main() {
        std::vector<std::string> data;
    
        data.push_back("one");
        data.push_back("two");
    
        if(contains(data, "two")) std::cout << "contains \"two\"\n";
        else std::cout << "does not contain \"two\"\n";
    
        if(contains(data, "four")) std::cout << "contains \"four\"\n";
        else std::cout << "does not contain \"four\"\n";
    
        return 0;
    }
    EDIT:

    Quote Originally Posted by Elysia
    I'd rather use a map. Searching a vector is a O(n) operation.
    It depends on the bigger picture too, of course.

    Quote Originally Posted by Elysia
    Unless it's sorted, which would mean you could use a binary search... but I don't think there IS such a search in the standard library.
    There is std::binary_search, std::lower_bound and std::upper_bound. std::binary_search would be the most appropriate in this proposed case.
    Last edited by laserlight; 09-12-2010 at 01:45 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    70
    I don't really know what you guys are saying because I'm only on classes right now. Any tips on getting better at Object Oriented Programming?

  14. #14
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Find appropriate objects to model and implement them correctly.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    To me it sounds like cuo741 is just trying to add something to the end of the vector.
    If that's the case, just use the push_back() function.
    Code:
    vector<string> items;
    ...
    items.push_back( "some item" );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed