Looping through strings in a vector and removing spaces

void getWords()
{
    string line;
    for (int i=0; i<assemb.size(); i++)
    {
        for (int word=0; word<assemb.at(i).size(); word++)
        {
            if (assemb.at(i).at(word) != ' ')
            {
                  getline(cin,line);
            }       

        } 
        fixedWords.push_back(line);
    }
}

I am trying to loop through this vector of strings and remove characters that are not spaces and add them to another vector, in my head this function should work but it doesn't it appears to get trapped in some sort of loop.

The words that at the moment print out as:

          VAR 0       
START:    LDN NUM01   
          SUB NUM02   
          STO MYSUM   
          LDN MYSUM   
          STO MYSUM   
END:      STP         
NUM01:    VAR 1025    
NUM02:    VAR 621     
MYSUM:    VAR 0 

However I want them to be:

-Var0
-Start:
-LDN
-NUM01
-SUB 
-NUM02
-STO
-MYSUM
-LDN
-MYSUM
-STO
-MYSUM
-END:
-STP
-NUM01
-VAR
-1025
-NUM02
-VAR
-621
-MYSUM
-0

This is part of an assembler program which takes a piece of assembler code turns it into binary commands and then inputs this into another program I have written which is able to read and execute binary commands.

Answers


How about this :

#include <iostream>
#include<vector>
#include<algorithm>
#include<iterator>
//#include<fstream>

using namespace std;

int main() {

std::vector<std::string> vec;
// std::ifstream fin("input.txt");
std::copy(std::istream_iterator<std::string>(std::cin), //fin
             std::istream_iterator<std::string>(),
             std::back_inserter(vec));

for(auto v:vec)
   std::cout<<"-"<<v<<std::endl;
return 0;
}

Replace std::cin with std::ifstream for the file

See here


It appears that what you want is pretty close to this:

std::copy(std::istream_iterator<std::string>(std::cin), 
          std::istream_iterator<std::string>(),
          std::ostream_iterator<std::string>(std::cout, "\n"));

The only difference is that this doesn't add the - at the beginning of each line. To do that, we can use transform instead of copy:

#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>

int main() {
    std::transform(std::istream_iterator<std::string>(std::cin),
        std::istream_iterator<std::string>(),
        std::ostream_iterator<std::string>(std::cout, "\n"),
        [](std::string const &in) { return "-" + in; });
    return 0;
}

Need Your Help

C++ Segmentation error(core dumped)

c++ segmentation-fault avl-tree

So the problem I'm having is a segmentation error.

jQuery: add() performance; is there a better way?

javascript jquery arrays performance add

What I want to do: Group all the like elements on a page (of a certain kind) into an object which I can later iterate on -- or apply sweeping changes to every element within.