0

I have a struct like this:

struct Stock{
    int id;
    string title;
    string colour;
    string size;
    int quantity;
    float cost;
}

Vector

vector<Stock> all_data;

Now I want to search for a specific stock in the all_data vector by the ID. How can I do this?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    You loop through the vector, checking each `Stock` inside to see if it's the right one. – Blaze Dec 04 '19 at 13:53
  • Apart from the straightforward answer of having an if statement in a loop, also take a look at [this So post](https://stackoverflow.com/questions/15517991/search-a-vector-of-objects-by-object-attribute) – kyriakosSt Dec 04 '19 at 13:57
  • 2
    [Edit](https://stackoverflow.com/posts/59177607/edit) the question and put the actual struct in it. `id` is _not_ a private member of `Stock` as the question now stands - which you only mention in a comment to one of the answers. – Ted Lyngmo Dec 04 '19 at 14:09

3 Answers3

4
for(auto data : all_data){
    if(data.id == id)
        std::cout << "Found";
}
RoQuOTriX
  • 2,871
  • 14
  • 25
  • 2
    @user11453590 You should add this to your question. BUT, the `struct` keyword in C++ means that (by default) all members of the struct are public. So something is clearly missing from your question – kyriakosSt Dec 04 '19 at 14:00
  • 3
    @user11453590 If `id` is a private member of `Stock`, then the `Stock` definition you showed us is different from the actual one. Please, do not waste our time and write your question more carefully using your actual code/case. – Daniel Langr Dec 04 '19 at 14:00
  • 1
    @DanielsaysreinstateMonica I agree, but please be friendlier. The user is clearly learning, it doesn't mean that they are careless. You are not helping either – kyriakosSt Dec 04 '19 at 14:04
  • 1
    @kyriakosSt asking to fix error in code that does not have the error is careless, what else? – 463035818_is_not_an_ai Dec 04 '19 at 14:05
  • 1
    @formerlyknownas_463035818 May be simply due to lack of knowledge, but even so, saying to a novice user (both in SO and evidently in programming) that they waste our time is not the way to go. – kyriakosSt Dec 04 '19 at 14:13
  • 1
    @kyriakosSt please dont turn everything into a discussion about friendlyness / welcomingness. Not showing the code that matches the error is wasting time and it has to be allowed to say this (also to a new user) – 463035818_is_not_an_ai Dec 04 '19 at 14:14
  • 3
    @formerlyknownas_463035818 I respectfully disagree for this specific case, but we can have different opinions. Sorry for starting this conversation – kyriakosSt Dec 04 '19 at 14:20
  • Thanks I solved the issue. Sorry for being unclear and my lack of knowledge. – user11453590 Dec 04 '19 at 14:25
3

You can use the standard algorithm std::find_if. For example

#include <vector>
#include <iterator>
#include <algorithm>

//…

int id = some_value;

auto it = std::find_if( std::begin( all_data ), std::end( all_data ),
                        [=]( const Staock &stocl )
                        {
                            return stock.id == id;
                        } );

if ( it != std::end( all_data ) )
{
    std::cout << it->id << '\n';
}

Instead of the lambda you could use the function object std::equal_to and std::bind provided that you are going to compare the whole objects of the type Stock or if you will declare a corresponding comparison operator.

Here is a demonstrative program that uses a lambda

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

struct Stock{
    int id;
    std::string title;
    std::string colour;
    std::string size;
    int quantity;
    float cost;
};

int main() 
{
    std::vector<Stock> all_data =
    {
        { 1, "A" }, { 2, "B" }, { 3, "C" }, { 4, "D" }, { 5, "E" }
    };

    int id = 3;

    auto it = std::find_if( std::begin( all_data ), std::end( all_data ),
                            [=]( const Stock &stock )
                            {
                                return stock.id == id;
                            } );

    if ( it != std::end( all_data ))
    {
        std::cout << it->id << ": " << it->title << '\n';
    }

    return 0;
}

Its output is

3: C
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You can try something like

it = std::find_if(all_data.begin(), all_data.end(), std::bind(&all_data::id, _1) == data.id);

from #include <algorithm>

Or, you can write your own function object

struct find_id : std::unary_function<Stock, bool> {
    int id;
    find_id(int id):id(id) { }
    bool operator()(Stock const& m) const {
        return m.id == id;
    }
};

it = std::find_if(alll_data.Stock.begin(), all_data.Stock.end(), 
         find_id(currentStockObject));

Modify as needed.

GokuMizuno
  • 493
  • 2
  • 5
  • 14