c++: how search data using a type?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1613 Views - Last Post: 22 July 2018 - 11:32 AM Rate Topic: **--- 3 Votes

#1 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

c++: how search data using a type?

Posted 20 July 2018 - 12:01 PM

imagine these structure:
struct person
{
   int age;
   string name;
   string adress;
   int phone;
};

person names[3];

now we can add 3 persons on names array.
i can search just using a name or C++ have some class's that help more?

This post has been edited by Cambalinho: 20 July 2018 - 12:02 PM

Is This A Good Question/Topic? 0
  • +

Replies To: c++: how search data using a type?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14425
  • View blog
  • Posts: 57,820
  • Joined: 12-June 08

Re: c++: how search data using a type?

Posted 20 July 2018 - 12:12 PM

How about a for loop?
Was This Post Helpful? 1
  • +
  • -

#3 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: c++: how search data using a type?

Posted 20 July 2018 - 12:19 PM

what i mean(sorry not explain better) is imagine if i can choose a name, age, adress or phone?
(search by 1 choosed type\structure item)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14425
  • View blog
  • Posts: 57,820
  • Joined: 12-June 08

Re: c++: how search data using a type?

Posted 20 July 2018 - 12:28 PM

Why not have a functions for all of them?

Allegedly 'find if' may work.
https://en.cpprefere.../algorithm/find
Was This Post Helpful? 1
  • +
  • -

#5 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: c++: how search data using a type?

Posted 20 July 2018 - 12:34 PM

int n1 = 3;
    int n2 = 5;
 
    std::vector<int> v{0, 1, 2, 3, 4};
 
    auto result1 = std::find(std::begin(v), std::end(v), n1);

correct me: the 3rd parameter accept any type? and search on name and adress(continue with my structure)?(both are strings)
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14425
  • View blog
  • Posts: 57,820
  • Joined: 12-June 08

Re: c++: how search data using a type?

Posted 20 July 2018 - 12:42 PM

Per the documentation what does it say? Type 't'..
Was This Post Helpful? 1
  • +
  • -

#7 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: c++: how search data using a type?

Posted 20 July 2018 - 01:24 PM

see my test:
struct person
{
   int age;
   string name;
   string adress;
   int phone;
};

vector<person> names;

person name2{10,"jesus", "fornelo", 96};

int main()
{
    names.push_back(name2);
    auto a=find(names.begin(), names.end(), "jesus");

"required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<person*, std::vector<person> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const char [6]>]'"
i get several errors... so what i'm doing wrong?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14425
  • View blog
  • Posts: 57,820
  • Joined: 12-June 08

Re: c++: how search data using a type?

Posted 20 July 2018 - 01:37 PM

I believe you need to write your own search predicate.. to tell it what fields to look at, etc.
Was This Post Helpful? 1
  • +
  • -

#9 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: c++: how search data using a type?

Posted 22 July 2018 - 06:55 AM

i did several changes and now works like i need ;)
class person
{
private:
    //for save all person class instances:
    static int intCount;
    static vector<person> vecPerson;
    
public:
    person()
    {
        //save all person class instances:
        intCount++;
        vecPerson.push_back((person)*this);
    }
    person(int intAge, string strName, string strAdress, int intPhone)
    {
        //for save all person class instances and get the inicializate data:
        intCount++;
        age=intAge;
        name=strName;
        adress=strAdress;
        phone=intPhone;
        vecPerson.push_back((person)*this);
    }
    
   int age;
   string name;
   string adress;
   int phone;

    //using a string or an integer for test if the item description exist:
   static int FindIndexFromSearch(string srchFind)
   {
       int intIndexSelected=-1;
       for(int i=0; i<intCount; i++)
       {
           if(srchFind==vecPerson[i].name || srchFind==vecPerson[i].adress)
           {
               intIndexSelected=i;
               break;
           }
       }
       return intIndexSelected;
   }

   static int FindIndexFromSearch(int srchFind)
   {
       int intIndexSelected=-1;
       for(int i=0; i<intCount; i++)
       {
           if(srchFind==vecPerson[i].age || srchFind==vecPerson[i].phone)
           {
               intIndexSelected=i;
               break;
           }
       }
       return intIndexSelected;
   }
};
int person::intCount=0;//count must be plus 1 of index
vector<person>person::vecPerson ;

sample:
vector<person> names;

person name2{10,"jesus", "fornelo", 96};
person name3{11,"joaquim", "forn", 93};
person name4{18,"joana", "forno", 94};
person name5{12,"jose", "viseu", 97};

int main()
{
    names.push_back(name2);
    names.push_back(name3);
    names.push_back(name4);
    names.push_back(name5);
    cout <<person::FindIndexFromSearch("jesus")<<endl;
    cout <<person::FindIndexFromSearch("joana")<<endl;
    cout <<person::FindIndexFromSearch("fornelo")<<endl;
    cout <<person::FindIndexFromSearch(93)<<endl;
    cin.get();
    return 0;
}

output:
0
2
0
1

now i'm getting trouble on destructor:
~person()
    {
        //test what instance was deleted and delete it from the vector:
        for(int i=0; i<intCount; i++)
       {
            if(vecPerson[i].name==name && vecPerson[i].adress==adress && vecPerson[i].age==age && vecPerson[i].phone==phone)
            {
               vecPerson.erase(vecPerson.begin() + i);
               intCount--;
               break;
            }
       }
    }

these destructor only test the actual instance with vector instances and then delete if is the same.
but i get a memory leak or something.
can you explain to me what i'm doing wrong or it's better create a new topic?
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 890
  • View blog
  • Posts: 3,592
  • Joined: 13-June 14

Re: c++: how search data using a type?

Posted 22 July 2018 - 07:53 AM

There are a couple of things that concern me here:

1. You should really learn how to use the standard library facilities. There's no need to go and write your own when a perfectly good function is there for you. I think it goes beyond this, actually. The STL has been designed in a particular way, so if you learn how to use parts of it, that knowledge will open you up to the other parts. In order to use a language well, you need to get into its mindset.

2. The names of your find methods really don't make it clear what they do. It's surprising, for example that FindIndexFromSearch(string srchFind) looks at both the name and adress fields (and similarly for the other method).

3. Why does your person class contain a vector<person> and a count? Those things don't represent a person and really have no business being there.

4. Why are you using global variables at all in your sample program? Those are to be avoided most of the time.

This post has been edited by ndc85430: 22 July 2018 - 08:11 AM

Was This Post Helpful? 3
  • +
  • -

#11 jimblumberg   User is offline

  • member icon

Reputation: 5569
  • View blog
  • Posts: 17,216
  • Joined: 25-December 09

Re: c++: how search data using a type?

Posted 22 July 2018 - 08:59 AM

Quote

2. Why does your person class contain a vector<person> and a count? Those things don't represent a person and really have no business being there.

An even bigger "hint" that something is seriously wrong is the "static" designation on that vector, it also seems that you don't understand that a vector
"knows" it's size.

You also need to learn to use class initialization lists instead of initializing inside the body of your constructors, and your data items shouldn't be in the public part of the class, use protected or private data members instead.

I'd start with something more like:
#include <string>


class person
{
    public:
        person() : age(0)
        {
        }
        person(int age, const std::string& name,
               const std::string& address,
               const std::string& phone) :
            age(age), name(name), address(address), phone(phone)
        {

        }
    private:
        int age;
        std::string name;
        std::string address;
        std::string phone;
};


#include <vector>

int main()
{
    std::vector<person> people;

}



Lastly, for now, if this class was designed properly you shouldn't need the destructor at all.


Edit: And please stop trying to in code the variable type into the variable name, C++ knows the type and if your variables are local and your functions are small there is no need to encode the type into the variable name. By the way just because people refer to things as a "number" doesn't mean that they should be treated as numbers in C++, as an example a "Phone Number" is really not a "number" it should be stored in a string not an integer.

Jim
Was This Post Helpful? 2
  • +
  • -

#12 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: c++: how search data using a type?

Posted 22 July 2018 - 09:09 AM

"Why does your person class contain a vector<person> and a count? Those things don't represent a person and really have no business being there."
because i need get the array items and search them.

"You should really learn how to use the standard library facilities. There's no need to go and write your own when a perfectly good function is there for you. I think it goes beyond this, actually. The STL has been designed in a particular way, so if you learn how to use parts of it, that knowledge will open you up to the other parts. In order to use a language well, you need to get into its mindset."
i don't know all STL true.

i did a diferent way, adding an index variable for not do the research:
class person
{
private:
    //for save all person class instances:
    static int intCount;
    static vector<person> vecPerson;
    int index=0;

public:
    person()
    {
        //save all person class instances:
        intCount++;
        index=intCount;
        vecPerson.push_back((person)*this);
    }
    person(int intAge, string strName, string strAdress, int intPhone)
    {
        //for save all person class instances and get the inicializate data:
        intCount++;
        index=intCount;
        age=intAge;
        name=strName;
        adress=strAdress;
        phone=intPhone;
        vecPerson.push_back((person)*this);
    }

    ~person()
    {
        //delete only if the index > 0:
        if(index>0)
        {
            vecPerson.erase(vecPerson.begin()+ index);
            intCount--;
        }

    }
///....

now the code works like i need... thank you so much.

theres 1 thing that i don't understand, even for a little samples: why you are so against the global variables?
i belive, in these case, isn't a problem

"By the way just because people refer to things as a "number" doesn't mean that they should be treated as numbers in C++, as an example a "Phone Number" is really not a "number" it should be stored in a string not an integer."
i'm confused: why use a string instead a number? is for use less memory or something?
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5569
  • View blog
  • Posts: 17,216
  • Joined: 25-December 09

Re: c++: how search data using a type?

Posted 22 July 2018 - 09:24 AM

Quote

"By the way just because people refer to things as a "number" doesn't mean that they should be treated as numbers in C++, as an example a "Phone Number" is really not a "number" it should be stored in a string not an integer."
i'm confused:


Nothing new about that you always seem to be confused.

Quote

why use a string instead a number? is for use less memory or something?

Because a "phone number" is not really a number, it is a series of dialing sequences. Don't you realize that a "phone number" can be represented by alpha characters as well as digits?

Quote

i did a diferent way,

Yes and as per usual, the hardest most illogical way possible.

Jim
Was This Post Helpful? 1
  • +
  • -

#14 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 890
  • View blog
  • Posts: 3,592
  • Joined: 13-June 14

Re: c++: how search data using a type?

Posted 22 July 2018 - 09:30 AM

View PostCambalinho, on 22 July 2018 - 05:09 PM, said:

"Why does your person class contain a vector<person> and a count? Those things don't represent a person and really have no business being there."
because i need get the array items and search them.


That makes no sense whatsoever. Why does needing a collection of elements require them to be defined in the class of the elements?

Quote

i don't know all STL true.


Well, you're using the STL a bit since vector is part of it. Really, take the time to learn how to use STL. You'd probably do well to get a copy of Stroustrup's "A Tour of C++", since that gives a high level overview of the language and how to use if effectively. Another option is "Programming -- Principles and Practice Using C++", which I think is meant to be more beginner friendly.

Quote

theres 1 thing that i don't understand, even for a little samples: why you are so against the global variables?
i belive, in these case, isn't a problem


It's acknowledged that using globals is generally a bad idea. Getting yourself into good habits in the easier cases will help you avoid it in the harder ones.

Quote

"By the way just because people refer to things as a "number" doesn't mean that they should be treated as numbers in C++, as an example a "Phone Number" is really not a "number" it should be stored in a string not an integer."
i'm confused: why use a string instead a number? is for use less memory or something?


No, it's not about memory. For a start, phone numbers tend to contain characters that aren't numeric ((), -, + and spaces, for example). It also doesn't make sense to perform mathematical operations with phone numbers like you can do with integers. Use a type that best represents the thing you're modelling. A string might be OK for this application, but later on, you may want to consider making your own type to represent phone numbers. On one level, this is core OO: using types to model your domain properly so that you can understand the application in an easier way. On another level, it gives you more type safety: with strings, you have the problem that all phone numbers are strings but the converse isn't true. This means you can put some arbitrary string where you only wanted a phone number and this can lead to bugs. With a dedicated class to represent phone numbers, the compiler just isn't going to let you do that.

This post has been edited by ndc85430: 23 July 2018 - 10:07 AM

Was This Post Helpful? 1
  • +
  • -

#15 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2647
  • View blog
  • Posts: 4,228
  • Joined: 21-June 11

Re: c++: how search data using a type?

Posted 22 July 2018 - 09:39 AM

Most importantly leading zeros matter for phone numbers and phone numbers may well contain more digits than fit into an int.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2