#Passing multiple vectors

32 messages · Page 1 of 1 (latest)

indigo turtle
#

Sup! I started getting back to C++ after 1,5 year break... How to pass multiple vectors to a function, but of indefinitive amount in c++?

It won't work:

template <typename T>
std::vector<T> Distinct(const std::vector<T>&... arrays)
{
    std::unordered_map<T, int> map;
    for (auto arr : arrays)
    {
        for (auto item : arr)
        {
            auto count = map[item];
            map.insert_or_assign(item, ++count);
        }
    }
        
    std::vector<T> result;
    for (auto item : map)
    {
        if (item.second > 1)
            result.push(item.first);
    }

    return result;
}
unkempt zincBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

rapid glade
#

A vector of vectors seems more straightforward.

indigo turtle
#

But then I can't do

auto nums = Distinct(nums1, nums2, nums3);
rapid glade
#

for (auto arr : arrays) copies your arrays (and doesn't work with that syntax).

indigo turtle
#

Is it possible even? I use c# on daily basis..

rapid glade
#

Do auto nums = Distinct({nums1, nums2, nums3}); instead.

indigo turtle
#

oh.... smart xD

#

Have to recall all of the syntax thx

#

I came up with this.. but how to prevent copying? Or is it being copied?

#include <iostream>
#include <vector>
#include <unordered_map>

template<typename T>
std::vector<T> Distinct(const std::vector<std::vector<T>>& arrays)
{
    std::unordered_map<T, int> map;
    for (auto arr : arrays)
    {
        for (auto item : arr)
        {
            auto count = map[item];
            map.insert_or_assign(item, ++count);
        }
    }

    std::vector<T> result;
    for (auto item : map)
    {
        if (item.second > 1)
            result.emplace_back(item.first);
    }

    return result;
}

int main()
{
    auto nums1 = std::vector<int>{ 3, 5, 6, 7, 8 };
    auto nums2 = std::vector<int>{ 1, 2, 3, 4, 5, 6, 7 };
    auto nums3 = std::vector<int>{ 19, 3, 8, 0 };

    auto vectors = std::vector<std::vector<int>>{ nums1, nums2, nums3 };
    auto nums = Distinct(vectors);
    for (auto item : nums)
        std::cout << item << "\n";
}





rapid glade
#

for (auto arr : arrays) makes a copy of each array. for (auto &arr : arrays) does not.

indigo turtle
#

What about when constructing std::vector<std::vector<int>> ? Does it allocate whole new memory?

#

I tried with some std::vector<std::vector<int>&>... but..

rapid glade
#

Yes, that also makes a copy.

#

Also you can do auto vectors = std::vector{ nums1, nums2, nums3 }; for shorter syntax, but it still makes a copy.

indigo turtle
#

auto vectors = std::vector{ nums1, nums2, nums3 }; well this spits error unfortunately

rapid glade
#

auto vectors = std::vector{ std::move(nums1), std::move(nums2), std::move(nums3) }; avoids the copies at the cost of making nums1 to nums3 empty.

indigo turtle
#

oh yeah did it right now

#

oh great thanks.. damn I got lazy with c#

#

I have a C++ technical interview on Thursday I don't know how I made it to there..

rapid glade
#

Well, let's hope "I know programming, but I haven't used C++ in a while" will be acceptable to them.

#

You can simplify

auto count = map[item];
map.insert_or_assign(item, ++count);

to

map[item]++;
indigo turtle
#

Yeah.. true.. and by the way.. do you often use functional approach in c++? Passing lambdas, std::for_each if there is any if I remember correctly... is it optimized or better to stick with normal for

rapid glade
#

I know almost nothing about functional approaches.

#

Lambdas are neat. Saves typing and keeps code localized. Very useful for things like sorting.

#

Algorithms are a bit of a wash. On one hand using them makes code more readable. On the other hand they often turn out not to be quite what you wanted and looking them up doesn't seem worth it. Personally I only use std::accumulate in practice and even that rarely. I think I used std::transform once or twice.

#

Optimization is a whole different topic. Don't needlessly copy big data structures, but other than that you can mostly ignore performance until you can't. Once it becomes relevant profile and benchmark, but in practice it will just not become relevant.

indigo turtle
#

Excellent

rapid glade
#

std::for_each seems like unnecessarily verbose syntax for a for-loop, but opinions differ. Some people are very strict about always using standard algorithms when possible, meaning you rarely write a regular for-loop.

unkempt zincBOT
#

@indigo turtle Has your question been resolved? If so, run !solved :)

indigo turtle
#

!solved