vector<int> to list of str

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanna88
    New Member
    • Oct 2009
    • 33

    vector<int> to list of str

    hello there,
    i would like to convert a list of vector int into a list of string where i need to add some space between those char in that string.

    what's on my mind is that to use sstream to convert each int into char and add space which definetely will be a few of if statement in my for loop

    But i think it would make my code less efficient in terms of time.correct me if i'm wrong.is there any better way of doing it?

    thanks.
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    Looks like the general idea cannot change, but how you implement it is what will determine the performance. Maybe we see some implementation then see if it can move faster.

    And do you want to insert a space (1) between every character?

    Regards,

    Alex

    Comment

    • hanna88
      New Member
      • Oct 2009
      • 33

      #3
      thanks for the reply.
      yes i need to add space between the char.
      here's the overview.. given v= 0123 and i need to convert it into string of
      v= "[0 1 2 3]".

      here's what i got so far
      Code:
      vector <int> v;
      
      if (!this->v.empty() ){
         for (int i=0; i<v.size(); i++){
      
           std::string s;
           std::stringstream out;
           out << v[i];
           s = out.str();
      
          //of course i need some if statement in order to add 
            "[" first then then spaces as well as a condition where no space 
            between the last char with the closing bracket.
      
           std::ans += s

      im pretty sure for loop will causes the code not to run faster in addition the conversion each int to each char.what can i do to improve it?

      Comment

      • myusernotyours
        New Member
        • Nov 2007
        • 188

        #4
        You can use the string member function insert() to add the spaces and whatever.

        Regards,

        Alex.

        Comment

        • hanna88
          New Member
          • Oct 2009
          • 33

          #5
          thanks again.i'll try :)

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Don't overlook the transform algorithm.

            You may find all you need to write is a function that converts a single vector element. The algorithm will call your function using every element of the vector.

            Comment

            Working...