-4

I have to combine both objects together and add the duplicated character's counts together.

class assign_obj{
    private:
        
        struct item{
            char value;
            int count;
        };
        std::vector<item> A;
        int size;
    public:
        //will combine the two objects together. If both objs have the same value the counts should be added together. 
        //this function should call compact on both objects before combining
        friend const assign_obj operator+(assign_obj &,assign_obj &);
};

const assign_obj operator+(assign_obj & obj1,assign_obj & obj2){
    for(int i = 0; i < obj2.A.size(); i++){
        obj1.A.push_back(obj1.item());  <---- issue here.
        //Add the unduplicated elements from obj2 to obj1
    }
    return obj1;
}

I already figured out how to add the duplicated counts but how do I add the unduplicated characters to one of the vectors?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Are looking to combine how to access an element in a vector, such as `obj2.A[i]` with how to insert into a vector, such as `obj1.A.push_back`, so then is `obj1.A.push_back(obj2.A[i]);` what you wanted? – TheUndeadFish Dec 01 '22 at 20:20
  • `issue here` What kind of issue? `Add the unduplicated elements` It isn't quite clear what this means exactly. An example input and desired output would help to clarify. But whatever it is, perhaps `std::vector` is not the right tool for this job. Also, do you really need all this entourage to ask this question? – n. m. could be an AI Dec 01 '22 at 20:23
  • 3
    Note that your `+` operator is performing `+=` behaviour by modifying the left-hand side. This will confuse the smurf out of people, so it's not a good plan. See [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/q/4421706/4581301) for details. – user4581301 Dec 01 '22 at 20:27
  • Please confirm or correct my understanding of the question: For each `item` in `obj2.A`, if the `item` already exists in `obj1.A` increase `count`. If not, add the `item` to `obj1.A`. Side question: If the `count` of an `item` in `obj2.A` is greater than 1, should its `count` be added to the `count` of the matching item in `obj1.A`? – user4581301 Dec 01 '22 at 20:33
  • 1
    Pretend that you implement a proper `operator+` that doesn't modify either of the operands, do you expect the same result if you do `obj1 + obj2` and if you do `obj2 + obj1`? Also: What does `compact` do? – Ted Lyngmo Dec 01 '22 at 20:44

1 Answers1

1

Your operator+ is implemented all wrong. It should look more like this instead:

assign_obj operator+(const assign_obj obj1, const assign_obj &obj2) {
    assign_obj result(obj1);
    for(const item &element2 : obj2.A){
        auto iter = std::find_if(result.A.begin(), result.A.end(),
            [&](const item &element1){ return element1.value == element2.value; }
        );
        if (iter != result.A.end())
            iter->count += element2.count;
        else
            result.A.push_back(element2);
    }
    return result;
}

If the order of the items is not important, then consider using std::(unordered_)map instead of std::vector, eg:

class assign_obj{
    private:
        std::map<char, int> A;
    public:
        friend assign_obj operator+(const assign_obj &, const assign_obj &);
};

assign_obj operator+(const assign_obj &obj1, const assign_obj &obj2) {
    assign_obj result(obj1);
    for(const item &element : obj2.A) {
        result.A[element.value] += element.count;
    }
    return result;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770