Sadique
Sadique

Reputation: 22841

Storing text from a file

My file contains the following code:

test.asm

^^^^^^^^^^^^^^^^

!START:
!LDA #97
!LDX #$A000
!STA, X
!END

I store the above code in my program in a string called sourcecode. Every Label (kind of a keyword for me here) begins with an ! mark. Thus, START / LDA / LDX / STA / END are labels which i would like to store. Now the contents of file may vary.

I would like to know which STL Container can i use to store my Keywords. Mind you each keyword is to be stored in single element of that container.

If vector can solve my problem, can an example be shown? My main aim is to use these labels/keywords to pass in functions to be interpreted as Mnemonics (which i will define).

Also, would like to know how would i iterate if my vector is of type string: vector<string>

Thanks in advance.

Upvotes: 0

Views: 155

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490623

You probably want to read the file with something like:

std::ifstream infile("yourfile.asm");

std::vector<std::string> lines;

std::copy(std::istream_iterator<line>(infile), 
          std::istream_iterator<line>(),
          std::back_inserter(lines));

If you want to filter out everything that does not start with a '!' at the same time, that's fairly easy too:

struct not_label { 
    bool operator()(std::string const &line) { 
       return line[0] != '!';
    }
};

std::remove_copy_if(std::istream_iterator<line>(infile), 
                    std::istream_iterator<line>(),
                    std::back_inserter(lines),
                    not_label());

Both of these use the line proxy I posted in a previous answer.

Edit: Contrary to (apparently) popular belief, attempting to minimize re-allocations of std::vector is rarely worthwhile. In reality, the allocation strategy used by std::vector assures that (on average) there is a fixed maximum number of times that items in the vector are copied -- and that number is low enough to surprise most people (around 3 for a typical implementation). Especially when you're doing I/O at the same time anyway, optimizing the vector reallocation is usually fairly pointless -- if you really want to optimize something, the I/O is nearly always the part that deserves concentration.

Upvotes: 0

VaidAbhishek
VaidAbhishek

Reputation: 6144

To decide upon a perfect container, follow this strategy.

1.) firstly, since you realize that your ultimate goal is to sort file full of some strings (which you call keywords), you should definitely use a contiguous memory container like vector or a deque, since running sort on a linked container (list, map) will be painstakingly slow. But since, you take the data from a file, you can't initialize the size of your vector in advance !!! This may cause you to keep stuffing new elements in the vector (with push_back()) and internally the vector has to copy its elements with every relocation of the container. Although, you can circumvent this problem, by ensuring the capacity of the vector to reflect the expected size of your input, but lets just assume that you don't have any idea on the input size.

2.) Now, given this situation a simple but effective strategy will be to first read the file and store all the keywords in a linked structure like list.

- list<string> myList ;

then, you insert the elements in this list one by one at the end. The good thing now is that no amount of insertion(s) will cause a reallocation like in the case of a vector.

Now, once you're done reading all the elements, you declare a vector of strings and in it's constructor supply the iterator range => myList.begin() and myList.end().

- vector<string> myVect(myList.begin(), myList.end()) ; // assigns all elements in myList to myVect.

this will effectively make a vector with same elements that is in the list, but now all the elements are in a contiguous manner and applying sort() algorithm will work like a charm !!!


A further level of optimization can also be done in this strategy. Since, once you have copied all the elements from the list to the vector, you can let go of the list and free its memory. To achieve this use this code snippet, after you have copied myList to myVect.

myList.clear(); // erases all elements from list

Upvotes: 1

holtavolt
holtavolt

Reputation: 4468

6502 assembly - what fun!

As tenfour says, a vector is good for compact and relatively immutable storage and access, but your usage over the lifetime of your app should guide your selection. (The good news is that if you use STL iterators, you should be able to switch container types with little impact on the rest of your code.)

Here's a couple pages to look at to help decide:

http://www.cplusplus.com/reference/stl/

http://en.wikipedia.org/wiki/Standard_Template_Library

Have fun!

Upvotes: 1

Related Questions