"std::vector<unsigned char>" - what is it?

B

Bobrick

Hi.

I'm in the process of making a GUI for a function someone else wrote,
and i've come across a type i'm unfamiliar with, namely
"std::vector<unsigned char>". I need to get the contents of this
variable into a form I can display in a text box, but i'm not sure what
to expect inside of the variable, whether I can just treat it like an
array e.t.c. Any help would be appreciated.

Thanks,

Bob
 
P

Peter Jansson

Bobrick said:
Hi.

I'm in the process of making a GUI for a function someone else wrote,
and i've come across a type i'm unfamiliar with, namely
"std::vector<unsigned char>". I need to get the contents of this
variable into a form I can display in a text box, but i'm not sure what
to expect inside of the variable, whether I can just treat it like an
array e.t.c. Any help would be appreciated.

Thanks,

Bob

It is a vector of unsigned char's. You can treat is as an array as follows:

std::vector<unsigned char> var;
const std::vector<unsigned char>::size_type N(var.size());
// Write it to cout and to a string:
std::eek:stringstream s;
for(int i=0;i<N;i++)
{
// Notice the
std::cout<<var<<'\n';
s<<var;
}
std::cout<<s.str()<<'\n';


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
C

Colander

Hi There,
I'm in the process of making a GUI for a function someone else wrote,
and i've come across a type i'm unfamiliar with, namely
"std::vector<unsigned char>". I need to get the contents of this
variable into a form I can display in a text box, but i'm not sure what
to expect inside of the variable, whether I can just treat it like an
array e.t.c. Any help would be appreciated.

If you have the time, have a look at the following page:
http://www.sgi.com/tech/stl/

If you have less time, look at:
http://www.sgi.com/tech/stl/Vector.html

Greets,
Bas
 
A

Alf P. Steinbach

* Peter Jansson:
Bobrick said:
Hi.

I'm in the process of making a GUI for a function someone else wrote,
and i've come across a type i'm unfamiliar with, namely
"std::vector<unsigned char>". I need to get the contents of this
variable into a form I can display in a text box, but i'm not sure what
to expect inside of the variable, whether I can just treat it like an
array e.t.c. Any help would be appreciated.

Thanks,

Bob

It is a vector of unsigned char's. You can treat is as an array as follows:

std::vector<unsigned char> var;
const std::vector<unsigned char>::size_type N(var.size());
// Write it to cout and to a string:
std::eek:stringstream s;
for(int i=0;i<N;i++)
{
// Notice the
std::cout<<var<<'\n';
s<<var;
}
std::cout<<s.str()<<'\n';


It's also possible that the vector is meant to directly represent a
string. In which case the simplest way to convert it to the string
format required by the OP's text box depends on that string format.
Converting to a std::string by copying (inefficient but works):

std::string s( v.begin(), v.end() );
 
J

Jim Langston

Bobrick said:
Hi.

I'm in the process of making a GUI for a function someone else wrote,
and i've come across a type i'm unfamiliar with, namely
"std::vector<unsigned char>". I need to get the contents of this
variable into a form I can display in a text box, but i'm not sure what
to expect inside of the variable, whether I can just treat it like an
array e.t.c. Any help would be appreciated.

Thanks,

Bob

The vector itself you can pretty much treat as an array. The contents of
the unsigned chars will be values from 0 to 255. What those represent, I
don't know. Are they characters? Are they values? You'll have to look at
the way it's used to determine that.

The fact that it's unsigned char instead of plain char would lead me to
suspect it's used for it's values. Many ways to iterate through a vector.
The one that most C programs would understand would be:

// presume Array is std::vector<unsigned char>
for ( size_t i = 0; i < Array.size(); ++i )
std::cout << Array << "\n";

Or you can iterate using iterators:
for ( std::vector<unsigned char>::iterator it = Array.begin(); it !=
Array.end(); ++it )
std::cout << *it << "\n";

In addition, an arrays memory is guaranteed to be contiguous.

unsigned char* vecp = &(Array[0]); // grab address of first unsigned char
for ( size_t i = 0; i < Array.size(); ++i )
std::cout << *(vecp + i) << "\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top