0
\$\begingroup\$

I wrote a simple data handler class.

I would like to know if I can make my class more simple and efficient for doing the identical feature.

My Code :

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class MyData
{
public:
    void add(const pair<string *, int> &elem)
    {
        size_t index = findIndex(elem.first);
        if (index != -1) //if key exists, update the value
        {
            myVec[index].second = elem.second;
            return;
        }
        myVec.push_back(elem);
    }
    void remove(string *strPtr)
    {
        size_t index = findIndex(strPtr);
        if (index != -1)
            myVec.erase(myVec.begin() + index);
    }
    void sort()
    {
        std::sort(myVec.begin(), myVec.end(), comp);
    }
    void print()
    {
        for (size_t i = 0; i < myVec.size(); ++i)
        {
            cout << *myVec[i].first << " : " << myVec[i].second << '\n';
        }
    }
private:
    size_t findIndex(string *strPtr)
    {
        auto it = find_if(myVec.begin(), myVec.end(), [&](pair<string *, int> const & ref)
                          {
                              return ref.first == strPtr;
                          });
        if (it != myVec.end())
            return std::distance(myVec.begin(), it);
        return -1;
    }
    static bool comp(const pair<string *, int> &a, const pair<string *, int> &b)
    {
        return a.second < b.second;
    }
    vector<pair<string *, int>> myVec;
};

int main()
{
    string fruits[] = {"Apple", "Banana", "Orange", "Grapes", "Lemon"};
    MyData data;
    data.add(make_pair(&fruits[0], 13));
    data.add(make_pair(&fruits[1], 52));
    data.add(make_pair(&fruits[2], 33));
    data.add(make_pair(&fruits[3], 8));
    data.add(make_pair(&fruits[4], 22));
    data.add(make_pair(&fruits[1], 17));
    data.sort();
    data.remove(&fruits[4]);
    data.print();
}

The Result :

Grapes : 8
Apple : 13
Banana : 17
Orange : 33
Program ended with exit code: 0
\$\endgroup\$
2
  • \$\begingroup\$ is there any reason to not use a standard container like std::map for the task? \$\endgroup\$
    – Sandro4912
    Aug 29, 2018 at 8:28
  • \$\begingroup\$ @Sandro4912 I couldn't sort values easily&efficiently using std::map so I decided to go with std::vector. Please see my other post: stackoverflow.com/questions/52055430/… \$\endgroup\$
    – Zack Lee
    Aug 29, 2018 at 8:44

1 Answer 1

2
\$\begingroup\$

Don't use using namespace std. Its considered bad practice.

See Why is “using namespace std” considered bad practice?

Why pass std::string by pointer? In C++ we have references &. They safe you the burden of dereference the pointers.

Dont use c-style arrays like

string fruits[] = {"Apple", "Banana", "Orange", "Grapes", "Lemon"};

In c++ you can use std::vector or std::array(if the size is fixed) for the task:

std::vector<std::string> fruits = {"Apple", "Banana", "Orange", "Grapes", "Lemon"};

Whats the whole purpose of the structure? You could just use a std::vector> and use std::find and std::sort to accomplish what you want to archieve with youre data structure.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.