Thread: Assigning the content of one map to another map and doing the same with vector

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by pianorain View Post
    Oh wow...I didn't know there is a programming term called "code smell". Thanks for the link.
    Yes, that's clear. However, you still haven't given us any code with which we can help you. Why not try something like this:
    Code:
    {//local stringstream instead of...whatever scope it's at now.
       std::stringstream currentEnumeratorValue_ss;
       currentEnumeratorValue_ss << currentEnumeratorValue_i;
       if (!currentEnumeratorValue_ss.good()) {
           cout<< "Not good..." <<endl;
           cout<< "Value was not added to stringstream." <<endl;
       }
       //use .get() to get the string
       currentEnumeratorValue_s = currentEnumeratorValue_ss.get();
       if (!currentEnumeratorValue_ss.good()) {
           cout<< "Not good..." <<endl;
           cout<< "Stringstream did not insert value into string." <<endl;
       }
    }
    output_s += currentEnumeratorValue_s;
    Using stringstream::get() didn't work either. It still failed at both inserting the int value of the enumerator into the stringstream, and outputting it to a string.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Should I be creating a new stringstream object each time I need one, instead of trying to use one object for all conversions of int to string? Also, do i need to pass anything to the stringstream's constructor, or is it ok to declare a stringstream like this:

    Code:
    stringstream currentEnumeratorValue_ss; //for converting the current enumerator value from an int to a string
    And then use the << operator right away to insert values into it. Because that's what I'm currently doing.
    Last edited by Programmer_P; 07-12-2010 at 07:21 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  3. #18
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Programmer_P View Post
    Using stringstream::get() didn't work either. It still failed at both inserting the int value of the enumerator into the stringstream, and outputting it to a string.
    I'm willing to bet that your stringstream isn't good even before your first integer insertion. You should check.
    Quote Originally Posted by Programmer_P View Post
    Should I be creating a new stringstream object each time I need one, instead of trying to use one object for all conversions of int to string?
    You can reuse the same one over and over:
    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
       std::stringstream s;
       for(int i = 0; i < 5; i++)
       {
          if(!s.good())
          {
             std::cout << "Not good - pre-insert" << std::endl;
          }
    
          s << i;
          if(!s.good())
          {
             std::cout << "Not good - post-insert" << std::endl;
          }
    
          std::string output = s.str();
          if(!s.good())
          {
             std::cout << "Not good - post-get()" << std::endl;
          }
          std::cout << output << std::endl;
          s.str(""); //clear the stringstream buffer
       }
    
       return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by pianorain View Post
    I'm willing to bet that your stringstream isn't good even before your first integer insertion. You should check.
    You were correct. It wasn't good before the first integer insertion dealing with the map.
    An error bit must have been set probably before that in the code, possibly when dealing with the enumerator values vector...
    Last edited by Programmer_P; 07-12-2010 at 07:44 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pianorain
    s.str(""); //clear the stringstream buffer
    If I remember correctly, you may also need to s.clear(); so as to reset any error flags the stringstream might be in. Personally, I think it is simpler to make the stringstream local to such a loop, unless due to performance reasons it turns out that you really need to pull it out of the loop.
    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

  6. #21
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by laserlight View Post
    If I remember correctly, you may also need to s.clear(); so as to reset any error flags the stringstream might be in. Personally, I think it is simpler to make the stringstream local to such a loop, unless due to performance reasons it turns out that you really need to pull it out of the loop.
    You are correct; you should s.clear() if you're going to reuse the stringstream. I'd also prefer to make the stringstream local; keep the code simple until it's finished, then optimize where you need it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #22
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Yep, this is where it first failed:

    Code:
    for (unsigned int i = 0; i < valuesOfEnumerators_v_i.size(); i++) {
          currentEnumeratorValue_i = valuesOfEnumerators_v_i.at(i); //get the current enumerator's value
          currentEnumeratorValue_ss<< currentEnumeratorValue_i;
          if (!currentEnumeratorValue_ss.good()) {
              cout<< "stringstream is no longer good. It failed when I attempted to insert an int into it.\n"
                          "We're at element " << i << " of the vector, which is: "
                     << valuesOfEnumerators_v_i.at(i) <<endl;
          }
          currentEnumeratorValue_ss>> currentEnumeratorValue_s;
          output_s += "    enumeratorValues.push_back(";
          output_s += currentEnumeratorValue_s; //add it to the output string
          output_s += ");\n";
    }
    Relevant output:

    stringstream is no longer good. It failed when I attempted to insert an int into it.
    We're at element 1 of the vector, which is: 0
    stringstream is no longer good. It failed when I attempted to insert an int into it.
    We're at element 2 of the vector, which is: 1
    stringstream is no longer good. It failed when I attempted to insert an int into it.
    We're at element 3 of the vector, which is: 2
    stringstream is no longer good. It failed when I attempted to insert an int into it.
    We're at element 4 of the vector, which is: 3
    stringstream is no longer good. It failed when I attempted to insert an int into it.
    We're at element 5 of the vector, which is: 4
    stringstream is no longer good. It failed when I attempted to insert an int into it.
    We're at element 6 of the vector, which is: 5
    EDIT: Ok, so it fails at the second element of the vector of some reason (which is a 0).
    Last edited by Programmer_P; 07-12-2010 at 08:12 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Does the stringstream not like it when a 0 is inserted into it?
    But that doesn't make any sense, since the first element of the vector was a 0 too, and that worked, while the second one failed...
    Oh, maybe its because I need to clear the stringstream after using it for each iteration of the loop.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #24
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Would you mind humoring me and checking to see if the stringstream is good after you use operator>>? I'm willing to bet it's not.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Oh yeah...
    Perfect. It works now.
    Bout' time...
    Seriously though, I don't get why a stringstream object wont overwrite any existing content when you use the << operator.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #26
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why should it magically clear()? You can append stuff to streams. It happens all the time with files... and cout...

  12. #27
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by pianorain View Post
    Would you mind humoring me and checking to see if the stringstream is good after you use operator>>? I'm willing to bet it's not.
    Yeah, its still good. The problem was apparently the stringstream << operator either does not handle well when you attempt to insert a value that already exists in the stream, and/or it it does not handle inserting into a stream which already has something in it.

    Either way, the problem is solved now.

    Thanks everyone.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #28
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Why should it magically clear()? You can append stuff to streams. It happens all the time with files... and cout...
    I thought cout outputs whatever argument was passed to its parameter, and then clears itself.

    An output like this:

    Code:
    int num = 10;
    cout<< "You suck this amount of times: "  << num << endl;
    I thought would first output the string, clear itself, output the number, clear itself, and then finally output the endl and clear itself. If it appends it, then why does using later cout statements without explicitly clearing the stream only output the new arguments being passed to cout?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #29
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Programmer_P View Post
    Yeah, its still good. The problem was apparently the stringstream << operator either does not handle well when you attempt to insert a value that already exists in the stream, and/or it it does not handle inserting into a stream which already has something in it.
    That doesn't seem right.
    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
       std::stringstream s;
       for(int i = 0; i < 5; i++)
       {
          if(!s.good())
          {
             std::cout << "Not good - pre-insert" << std::endl;
          }
    
          s << 0;
          if(!s.good())
          {
             std::cout << "Not good - post-insert" << std::endl;
          }
    
          std::string output = s.str();
          if(!s.good())
          {
             std::cout << "Not good - post-get()" << std::endl;
          }
          std::cout << output << std::endl;
       }
    
       return 0;
    }
    The output is:
    Code:
    0
    00
    000
    0000
    00000
    As you can see, it has both a repeated value and adding to a non-empty stringstream.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  15. #30
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by pianorain View Post
    That doesn't seem right.
    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
       std::stringstream s;
       for(int i = 0; i < 5; i++)
       {
          if(!s.good())
          {
             std::cout << "Not good - pre-insert" << std::endl;
          }
    
          s << 0;
          if(!s.good())
          {
             std::cout << "Not good - post-insert" << std::endl;
          }
    
          std::string output = s.str();
          if(!s.good())
          {
             std::cout << "Not good - post-get()" << std::endl;
          }
          std::cout << output << std::endl;
       }
    
       return 0;
    }
    The output is:
    Code:
    0
    00
    000
    0000
    00000
    As you can see, it has both a repeated value and adding to a non-empty stringstream.
    Maybe its a compiler specific issue then? I'm using GCC. What compiler are you using?
    I'm about to try to compile your code in my compiler and see if I get the same results.

    At any rate, clearing the stringstream after each operation in the loops fixed the problem (note that I call clear() after using both the << and >> operators). I don't know why, but it did.

    EDIT: Hmm...I got the same results you did.
    Last edited by Programmer_P; 07-12-2010 at 08:38 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed