user2754070
user2754070

Reputation: 507

Element access (get and put) using std::vector in C++

I am trying to manipulate elements of file (.txt) using vector - such as push_back, accessing [] etc. I want to perform a simple encoding on the elements I retrieved from the file, I succeeded partially though the end result is not what I expected.

Here is the file content (sample):

datum
-6353
argo

What I wanna perform here is read each and every element of the vector (kinda 2-D) and encode them with their respective ASCII codes.

Here is my code (so far):

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

    using namespace std;

    int main () 
    {
        std::string word;
        //std::vector<std::vector <std::string> > file;
        std::vector<std::string> file;

        std::ifstream in( "test.txt" );

        while (in >> word)
        {
            file.push_back(word);
        }

        for (size_t i=0; i<file.size(); i++)
        {
           for (int j=0;j<file[i].size();j++)
           {
               //cout<<i<<","<< j<<endl;
               cout<<((int)file[i][j])<<",";
           }
        }

        in.close();

  return 0;
}

Present Output (for the above code):

100,97,116,117,109,45,54,51,53,51,97,114,103,111,
RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

Expected Output:

100,97,116,117,109
45,54,51,53,51
97,114,103,111

Please guide me to achieve this.

Upvotes: 3

Views: 367

Answers (2)

woosah
woosah

Reputation: 853

You already picked a solution, but thought I'd put this out there and make use of the C++ language. Since you seem to have #include <iterator> in your above problem and you included tag C++, I thought a more elegant solution using iterators etc would be a worth while post - though you already picked an answer.

template <typename T>
void printToScreen (std::vector<T> myVec, char delim = ',')
{
    typename std::vector<vector<T>>::const_iterator vit_x;
    typename std::vector<T>::const_iterator vit_y;
    for (vit_x = myVec.begin(); vit_x < myVec.end(); vit_x++) {
        for (vit_y = vit_x->begin(); vit_y < vit_x->end(); vit_y++) {
            std::cout << *vit_y << delim;
        }
        // remove last delimiter and add new line to console
        std::cout << "\b \n";
    }
}

then in your main function you can do the following:

printToScreen (file);

or if you want another delimiter char such as '.'

printToScreen (file,'.');

If you have C++11, the function can be further simplified

template <typename T>
void printToScreen(const std::vector<std::vector<T>> &myVec, char delim = ',')
{
    for (auto i : myVec) {
        for (auto val : i) {
            std::cout << val << delim;
        }
        std::cout << "\b \n";
    }
}

One can further simplify this using C++11 lambda function. This is just an example for 2D. Maybe even change the delim argument as a string so the delimiter is a multi-character sequence then generating '\b' special character for the size of string.

Testing:

std::vector<std::vector<int>> d1{{1,2,3},{2,3,4,7,6}, {3,4,5,2,1}};
std::vector<std::vector<std::string>> d2{{"Testing","function","prettyToScreen"},{"I'm","making","use of","the"}, {"C++","language"}};
 printToScreen(d1);
 std::cout << endl;
 printToScreen(d2, ' ');

output:

1,2,3
2,3,4,7,6
3,4,5,2,1

Testing function prettyToScreen 
I'm making use of the 
C++ language 

Upvotes: 1

Digital_Reality
Digital_Reality

Reputation: 4738

Try cout << endl after first loop

for (size_t i=0; i<file.size(); i++)
{
   for (int j=0;j<file[i].size();j++)
   {
       //cout<<i<<","<< j<<endl;
       cout<<((int)file[i][j])<<",";
   }
  **cout << endl;**
}

Upvotes: 2

Related Questions