By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,667 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jan 8 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Bobrick wrote:
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 charvar;
const std::vector<unsigned char>::size_type N(var.size());
// Write it to cout and to a string:
std::ostringstream s;
for(int i=0;i<N;i++)
{
// Notice the [i]
std::cout<<var[i]<<'\n';
s<<var[i];
}
std::cout<<s.str()<<'\n';
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Jan 8 '07 #2

P: n/a
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

Jan 8 '07 #3

P: n/a
* Peter Jansson:
Bobrick wrote:
>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 charvar;
const std::vector<unsigned char>::size_type N(var.size());
// Write it to cout and to a string:
std::ostringstream s;
for(int i=0;i<N;i++)
{
// Notice the [i]
std::cout<<var[i]<<'\n';
s<<var[i];
}
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() );

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 8 '07 #4

P: n/a
"Bobrick" <pu******@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
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[i] << "\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";
Jan 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.