STL Map Problem with user defined Class

This is a discussion on STL Map Problem with user defined Class within the C++ Programming forums, part of the General Programming Boards category; >> But I dont need that so whats the alternative ?? If you don't care what order they're in, then ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> But I dont need that so whats the alternative ??
    If you don't care what order they're in, then you should still use the map and let it order them.

    If you don't want them to be in alphabetical order, but any other seemingly random order will do, then you can use a hash table (like unordered_map) which will allow fast lookups without ordering the elements.

    If you want them to be in the same order you insert them, then you'll have to use a vector (or maybe a deque or list) and combine the key and value into a single struct. The problem is then that you won't be able to do lookups quickly because the entire container will have to be searched. If you don't plan on doing lookups, then this shouldn't be a problem.

    If you want fast lookups and specific ordering, then you'll need some combination of containers to hold that information.

  2. #17
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    are all these overloads valid ??
    Code:
    Var(const double val);
    Var(const float val);
    Var(const int val);
    Var(const short val);
    Var(const unsigned float val);
    Var(const unsigned int val);
    Var(const unsigned long val);
    Var(const unsigned short val);
    Edit: especially for the float and double type.
    Last edited by noobcpp; 07-21-2008 at 10:20 AM.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, there are no unsigned floats.
    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

  4. #19
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    What ??????????
    floats cant be unsigned ?? never knew before
    the same applies for double too Right ??
    But why they cant be unsigned ??

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Floats and doubles cannot be unsigned (nor can they be signed with the keyword signed).
    Although, if I were you, I'd study basic templates since they would simplify much of your code.
    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. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by noobcpp View Post
    What ??????????
    floats cant be unsigned ?? never knew before
    the same applies for double too Right ??
    But why they cant be unsigned ??
    Because it wouldn't make any sense to do so (where should the extra bit go -- significand or exponent? If significand, you've not extended the range, so what's the point; if the expnoent, you'll have to change the bias, which means a whole new set of algorithms to actually do the math, and it's not worth it).

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Although, if I were you, I'd study basic templates since they would simplify much of your code.
    Yes, from a cursory look at your work it seems that you simply want a class that can hold anything as its string representation.

    Actually you might not even need a separate class. What you need is something to turn stuff into their string representation and back. Boost libraries have lexical_cast for that, but it wouldn't be hard to write your own with stringstreams. (The difference with the C conversion functions is that you can turn anything, including user-defined classes, into string and back as long as operators << and >> have been overloaded for them.)

    Most basic (more error checks could be added):
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    template <class From, class Into>
    bool convert(const From& from, Into& into)
    {
        std::stringstream ss;
        ss << from;
        return ss >> into;
    }
    
    //example usage
    int main()
    {
        //convert long into string
        long n = 42;
        std::string s;
        convert(n, s);
    
        //convert string into int
        int i;
        if (convert(s, i)) {
            std::cout << "Success: " << i << '\n';
        }
        else {
            std::cout << "Failed to convert " << s << " into int.\n";
        }
    }
    Last edited by anon; 07-21-2008 at 12:08 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 12:29 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  4. Static member (map) / constructor initialisation problem
    By drrngrvy in forum C++ Programming
    Replies: 9
    Last Post: 12-28-2005, 11:03 AM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

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