Structures, Vectors, Members oh my (STUCK)

I understand completely the idea of the Struct and Vector but when I put two and two together it always screws me over.

In this first portion of code, I want to use the global variable vector <cd> myCollection (which contains structs of members which are music related- group, label, album, year, price) to check to see if it matches with what the user is searching for in a temporary struct(artist_search.group).

I hope this all makes sense, I'm a beginner but this all just doesn't seem right the way I'm trying. If there's an easier way to take what the user is searching for and comparing it to a vector of structs and then returning all the info from a different vector of structs...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  vector <cd> artist() 
{
    int numfound=0;
    vector <cd> artists_found; //final vector that will be returned with the found artist, and all of their other info aswell (may have more than one)
    cd artist_search; //temporary struct to get users input
	cout<<"\nWhat artist are you looking for?:  ";
	cin.ignore(80,'\n');
	getline(cin, artist_search.group); //collects users input, how could i put a vector here instead?

	cout<<artist_search.group;

    for(int i=0; i<myCollection.size(); i++)
    {
        if(myCollection[i].group == artist_search.group) //if the vector position group matches the struct group it does the following
        {
            artists_found[numfound].group=myColleciton[i].group;  //inserts the existing myCollection vector data into a new vector with only the searched artists info
            artists_found[numfound].album=myCollection[i].album;
            artists_found[numfound].label=myColleciton[i].label;
            artists_found[numfound].year=myColleciton[i].year;
            artists_found[numfound].price=myColleciton[i].price;
            numfound++;
            artists_found.resize(artists_found.size() +1);

        }
    }
return artists_found;
}
Last edited on
So you are looking for an easier way of returning certain information contained within a struct being held in a vector?

There are ways to increase efficiency when searching through any data structure.
Choosing the correct ADT...
Choosing the correct search...

But there are none that make it "easier". You still have to go through the process of

1.) Taking user's information
2.) Accessing/Finding information in the vector.
3.) Extracting found information and presenting to the user.

It might be that this is not working as you have it because of the comparison's you are doing between structs.

C++ does not have a default "==" operator for a struct, you have to overload the "==" operator to return a result you can use. Maybe that was what was giving you the difficult time?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct cd
{
    std::string artiste ;
    std::string album ;
    std::string label ;
    int year ;
    double price ;

    // ...
};

std::vector<cd> find_cds_by_artiste( const std::string& artiste_name, const std::vector<cd>& myColleciton )
{
    std::vector<cd> result ;

    for( const cd& this_cd : myColleciton ) // http://www.stroustrup.com/C++11FAQ.html#for
        if( this_cd.artiste == artiste_name ) result.push_back(this_cd) ;

    return result ;
}

// or, legacy c++
std::vector<cd> find_cds_by_artiste_2( const std::string& artiste_name, const std::vector<cd>& myColleciton )
{
    std::vector<cd> result ;

    for( std::size_t i = 0 ; i < myColleciton.size() ; ++i ) // classical for
        if( myColleciton[i].artiste == artiste_name ) result.push_back( myColleciton[i] ) ;

    return result ;
}
In my if statement, it's giving me the error saying that "myCollection" wasn't declared in the scope. But it's a global variable that already has data in it. I think I'm writing this statement-
artists_found[numfound].group=myColleciton[i].group; - incorrectly, and I don't know how to set my new vector member group equal to myCollection vectors member group if that makes sense.
> it's giving me the error saying that "myCollection" wasn't declared in the scope.
> But it's a global variable that already has data in it.

Declare it. Like this: extern std::vector<cd> myCollection ;


> I think I'm writing this statement-
> artists_found[numfound].group=myColleciton[i].group; - incorrectly

vector <cd> artists_found; is created as an empty vector; its size is zero.
To add an item to it, use push_back().
artists_found[numfound] is valid only if artists_found.size() is greater than numfound
Topic archived. No new replies allowed.