Thread: switching betwen container types

  1. #1
    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,895
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,895
    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,366
    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