switching betwen container types

This is a discussion on switching betwen container types within the C++ Programming forums, part of the General Programming Boards category; Hello In my application I mostly use functions such as break_string(), break_file_by_lines() that return std::vector<std::string> (vector of strings) as a ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    switching betwen container types

    Hello

    In my application I mostly use functions such as break_string(), break_file_by_lines() that return std::vector<std::string> (vector of strings) as a result.

    Now I have to store the result as a std::set container and I'm wondering what is the best way to convert betwen container types?

    Is there any special function that I should use so that preformance wouldnt be affected too much or should I maybe use some of the template techniques for the functions mentioned above?

    Many thanks in advance!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You want to store the elements of a vector in a set?
    Code:
    vector_t v = get_vector();
    set_t s(v.begin(), v.end());
    Of course, this will copy the data at least twice. But there's nothing to be done about that until we get move constructors.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    But there's nothing to be done about that until we get move constructors.
    Yes, unless you are willing to change those functions, but if they work and the additional copying is not a problem, then there may be no point in doing that, at least for now.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is it possible to make functions break_string() templated, so that they can return set/vector or something else as a result?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Well, yes it is, provided they have the same interface - ie same functions.
    But it seems to me it won't work.
    std::set doesn't support push_back.
    std::vector supports insert, but takes two arguments instead of one.
    Btw, this is typically called static polymorphism.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You could supply an output iterator to break_string, instead of having it return a container. Then it would work both std::vector (use a back_insertion_iterator) and std::set (use an insertion_iterator).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    And as I mentioned in the other thread, you might consider just leaving the data in the vector and using algorithms to make it unique.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. List or container to hold types
    By tony_chestnut in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2006, 11:46 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21