Can I rethink std :: vector <char> as std :: vector <unsigned char> without copying?

I have a link to std::vector<char>which I want to use as a parameter for a function that takes std::vector<unsigned char>. Can I do this without copying?

I have the following function and it works; however, I'm not sure if the copy is really happening - can someone help me figure this out? Can it be used std::moveto avoid copying, or is it no longer being copied?

static void showDataBlock(bool usefold, bool usecolor,
            std::vector<char> &chunkdata)  
{
  char* buf = chunkdata.data();                      
  unsigned char* membuf = reinterpret_cast<unsigned char*>(buf); 
  std::vector<unsigned char> vec(membuf, membuf + chunkdata.size()); 
  showDataBlock(usefold, usecolor, vec);   
} 

I thought I could write:

std::vector<unsigned char> vec(std::move(membuf),
                               std::move(membuf) + chunkdata.size());  

Is this an excess? What is really going on?

+4
source share
6 answers

... std:: move, ,

. a std::vector<char> a std::vector<unsigned char>. , , "" O (1) .

:

void showData( std::vector<char>& data){
    std::vector<unsigned char> udata(data.begin(), data.end());
    for(auto& x : udata)
        modify( x );
    ....
}

...

inline unsigned char& as_uchar(char& ch){
    return reinterpret_cast<unsigned char&>(ch);
}

void showDataBlock(std::vector<char>& data){
    for(auto& x : data){
        modify( as_uchar(x) );
    }
}
+3

, : -

showDataBlock(usefold, usecolor, std::vector<unsigned char> & vec);  

std::vector<T> std::vector<T2>.

.

std::vector , , .
: std::vector.
, .
  , , .

...

std::move(membuf)

... = . ( , membuf)

, : std::vector<char> std::vector<unsigned char> .

C, char, unsigned char? (, C::getChar() C::getUnsignedChar(), ... char, )

, .
, .

, .
, .

+1

v1 std::vector<T1> v2 std::vector<T2>, , T1 T2 "", char unsigned char.

:

std::vector<unsigned char> v2;
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));

- - : std::vector<T2> , std::vector<T1> (, ). (), [contigous].


, reinterpret_cast std:: move
,
, - ?

( ) . .

, , release(), () / , ( ) . ( reinterpret_cast, ... )

std::vector . , . .

+1

, showDataBlock.

, :

  • showDataBlock, signed char, unsigned char (.. )
  • , - . ( value_type char) , signed char unsigned char elementwisely.
+1

unsigned char char - . , ( , ), reinterpret_cast .

static void showDataBlock(bool usefold, bool usecolor,
            std::vector<char> &chunkdata)  
{
  showDataBlock(usefold, usecolor, reinterpret_cast< std::vector<unsigned char>&>(chunkdata));   
}

, , , , . , , , char.

0

- :

static void showDataBlock(bool usefold,bool usecolor, std::vector<char> chunkdata)
{                                                                                                                           
    std::vector<unsigned char>&cache = reinterpret_cast<std::vector<unsigned char>&>(chunkdata);                                              
    showDataBlock(usefold, usecolor, cache);    
}                                                                             

static bool showDataBlock(bool usefold,bool usecolor, std::vector<unsigned char> &chunkdata)   
{
    // showing the data
}

This solution allowed me to pass the vector as ref or as usual it seems that it works - if I don’t know its best solution, however you all came up with some really good suggestions - thank you all

I agree that I can’t avoid copying, so I allow copying with normal parameter passing

Please, if you find that this is a wrong decision, then indicate the comment in the comment, not just downvote

-3
source

Source: https://habr.com/ru/post/1665723/


All Articles