Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to create a vector from 2D vector and without the duplicate elements in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <memory>
#include <bitset>
#include <iterator>

int main() {
    std::vector<std::vector<int>> vec2d {
        { 1, 4, 8, 9 },
        { 4, 7, 4, 3 },
        { 4, 1, 1, 3 }
    };

    int const vsize = 12; 
    auto exist = std::make_unique<std::bitset<vsize + 1>>();
    
    std::vector<int> vec;
    for (auto const& v1d : vec2d) {
        for (auto const val : v1d) {
            if (!exist->test(val)) {
                vec.emplace_back(val);
                exist->set(val, true);
            }
        }
    }
    
    std::copy(vec.cbegin(), vec.cend(), std::ostream_iterator<int>(std::cout, "\n"));
}





/*
run:

1
4
8
9
7
3

*/

 



answered Nov 18, 2022 by avibootz

Related questions

1 answer 84 views
1 answer 154 views
1 answer 198 views
2 answers 164 views
1 answer 96 views
96 views asked May 12, 2021 by avibootz
...