-1

I have k (0 < k < 8) CSV files containing values all 0 or 1.

My C++ code reads from the file and stores the content of each file into a vector<signed char>.

I wished to merge (concat) then store them in a single vector<signed char>.

File 1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0     Stored in vector1          
File 2: 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0     Stored in vector2        
File 3: 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0     Stored in vector3            
File 4: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0     Stored in vector4          
File 5: 1 1 0 1 1 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0     Stored in vector5           

I wished to store them in vector<signed char> vectork:

  • with vectork[0] stored where each element has bit pattern as [0 0 0 0 1 0 1 1] -- first col
  • with vectork[1] stored where each element has bit pattern as [0 0 0 0 1 1 0 1] -- second col
  • with vectork[2] stored where each element has bit pattern as [0 0 0 0 1 0 0 0] -- third col

I tried with

    vectork.resize(vector1.size(),0);
    for ( int i = 0; i < vector1.size(); i++ ) {
       vectork[i] = vectork[i] << 1;
      if (vector1[i] == 1) vectork[i] +=1;
      vectork[i] << 1;
      if (vector2[i] == 1) vectork[i] +=1;
      vectork[i] << 1;
      if (vector3[i] == 1) vectork[i] +=1;
      vectork[i] << 1;
      if (vector5[i] == 1) vectork[i] +=1;
    }

Is the above correct?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
letsBeePolite
  • 2,183
  • 1
  • 22
  • 37

3 Answers3

0

This would be a lot easier done with bitsets, however, if you choose to do it this way, it would look something like this.

I'm still kind of confused as to what exactly you're trying to do, but it seems like you're trying to get all of those vectors into one two dimensional vector (you did as an array of vectors, but I feel as if you intended it like this).

This will get all the vectors, and append them into a new vector of vectors.

// Example program
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<signed char> vector1 = {0,1,0,1,0,1,0,1};
    vector<signed char> vector2= {0,0,0,1,0,1,0,1};
    vector<signed char> vector3= {0,1,1,1,0,1,0,1};
    vector<signed char> vector4= {1,1,0,1,0,1,0,1};
    vector<signed char> vector5= {1,0,0,1,0,1,0,1};

    vector<vector<signed char>> vectork(5, vector<signed char>(8));
    vectork.clear();
    vectork.push_back(vector1);
    vectork.push_back(vector2);
    vectork.push_back(vector3);
    vectork.push_back(vector4);
    vectork.push_back(vector5);


    //to check if it correctly works (it does).
    for(vector<signed char> v : vectork) {
        for(signed char i : v) {
            printf("%d ", i);
        }
        printf("\n");
    }
}

The output would look like this:

0 1 0 1 0 1 0 1 
0 0 0 1 0 1 0 1 
0 1 1 1 0 1 0 1 
1 1 0 1 0 1 0 1 
1 0 0 1 0 1 0 1 

Let me know if you were trying to do something slightly different and I can tweak it for you, or if you need any explanation on what I wrote, or have any questions in general.

Robin Singh
  • 161
  • 1
  • 5
0

Firstly I would recomend that you use std::vector<bool> instead of std::vector<signed char> This is specifically for performance reasons as your compiler will likely reduce the memory usage by storing 8 Booleans in 1 byte as apposed to storing 1 Boolean in a byte. Secondly I think you have completely misunderstood the process of bit shifting, as far as I am aware from what you have posted you are simply trying to concatenate two STL Vectors this has been asked here. For reference I have included a code snippet from the excellent answer by Robert Gamble. In your

vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
Community
  • 1
  • 1
silvergasp
  • 1,517
  • 12
  • 23
0
#include <vector>

std::vector<signed char>
merge(const std::vector<signed char>& vector1,
      const std::vector<signed char>& vector2,
      const std::vector<signed char>& vector3,
      const std::vector<signed char>& vector4,
      const std::vector<signed char>& vector5)
{
    std::vector<signed char> result;
    result.reserve(vector1.size());

    auto i1 = vector1.begin();
    auto i2 = vector2.begin();
    auto i3 = vector3.begin();
    auto i4 = vector4.begin();
    auto i5 = vector5.begin();

    while (i1 != vector1.end()) {
        int n = 0;
        for (auto *v: { &i1, &i2, &i3, &i4, &i5 })
            n = n*2 + *(*v)++;
        result.push_back(n);
    }

    return result;
}

// test it:
#include <algorithm>
int main()
{
    auto v = merge({ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                   { 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
                   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                   { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                   { 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0 });

    auto expected = { 0b1011, 0b1101, 0b1000, 0b0001, 0b0001, 0b0000, 0b0000, 0b0000,
                      0b0001, 0b1001, 0b1001, 0b0000, 0b0000, 0b0000, 0b1001, 0b0000,
                      0b0000, 0b0000, 0b1001, 0b0000 };

    return !std::equal(expected.begin(), expected.end(), v.begin());
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103