1

I need to add a comma to each string stored in the array according to a position which is stored in the byteSizeBuffer.

std::string::iterator it;
    int position = 0;
    int totalSize = 0;
    for (int i = 0; i < numOfMPs+1; i++)
    {
        for (int j = 0; j < numOfMPs; j++)
        {
            if(totalSize < subSize)
            {
                switch(byteSizeBuffer[j]) {
                    case 2:
                        position = totalSize+4; //4 because in the string each byte is 2 characters (2*2=4)
                        totalSize = position;
                        it = sub_[i].begin()+position, ','; //inserts comma at iterator+position
                        break;
                    case 4:
                        position = totalSize+8;
                        totalSize=position;
                        it = sub_[i].begin()+position, ',';
                        break;
                    default:
                        cout << "An error has occured while splitting the Data";
                        break;
                }
            }
        }
    }
  1. The position is the position which i want the comma to be inserted.
  2. The totalSize is the total of the positions and cannot exceed subSize
  3. subsize is always and even number and always 34 bytes ~68 characters
  4. The commas are added to the same position for every string in the array.

Strings look like: FG454F3423T5245G4G5H6546456Y54645G4456G45G60101000000101010111000001

The positions stored in the bytesizeBuffer[] are stored as either a 2 or a 4 which represent the number of bytes.

So if the numbers stored were 4,4,4,4,2,4,4,2,2,2,2 I need it to look like: FG454F34,23T5245G,4G5H6546,456Y5464,5G44,56G45G60,10100000,0101,0101,1100,0001 ..........No comma should be added at the end of the line.

My above code does not seem to work and i was wondering if i was going about it the correct way or if maybe there was a more efficient way of doing the above.

Just some tips/pointers to get me on the right track is really what i'm looking for. Thanks

5
  • Just loop through the bytesizeBuffer constructing a second string from the first. I have no idea why you think you need a nested loop! Commented Jan 14, 2014 at 14:20
  • it = sub_[i].begin()+position, ','; //inserts comma at iterator+position. Apart from assining a new value to it, this does nothing. Commented Jan 14, 2014 at 14:21
  • @Roddy I have 2 arrays. the first holds multiple strings and the second holds multiple positions where the commas need to go. Therefore the first loops through the string array and the second loops through the byteSizeBuffer (or the position array). Commented Jan 14, 2014 at 15:08
  • Ah, so you've got to insert commas in the same spaces in each of many strings? That wasn't clear. Start by writing a function that does the job for one string, then call that function in a loop. Commented Jan 14, 2014 at 15:14
  • Added the new code in an answer. It works. Commented Jan 14, 2014 at 16:07

2 Answers 2

1

You can use std::string::insert.
http://en.cppreference.com/w/cpp/string/basic_string/insert

Also, don't forget that after each insertion your string size changes and hence the positions of all proceeding elements.

Sign up to request clarification or add additional context in comments.

Comments

0
std::string::iterator it;
    int position = 0;
    int totalSize = 0;
    for (int i = 0; i < numOfMPs+1; i++)
    {
        for (int j = 0; j < numOfMPs; j++)
        {
            /* mp_subSize*2: each byte is 2 chars long so we have to get the full 
             * length of the string according to the characters 
             * +numOfMPs-1: adds the numOfMPs and then subtracts the final size from the buffer and -1 for the comma
             * so that there is no comma added at the end of the string.
            */
            if(totalSize < _subSize*2+numOfMPs-(byteSizeBuffer[count-1]*2)-1)
            {
                switch(byteSizeBuffer[j]) {
                    case 2:
                        position = totalSize+4; //4 because in the string each byte is 2 characters (2*2=4)
                        totalSize = position+1; //number of bytes +1 for the comma
                        break;
                    case 4:
                        position = totalSize+8;
                        totalSize=position+1;
                        break;
                    default:
                        cout << "An error has occured while splitting the Data";
                        break;
                }
                //this line adds the comma
                it = sub_[i].insert(sub_[i].begin()+position, ',');
            }
        }
        totalSize=0;
        position=0;
    }

A more efficient way would be to put this into a function like @Roddy said in the comments. However, this is what i came up with and it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.