473,511 Members | 14,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 19961
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
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
* 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4092
by: Karl Ebener | last post by:
Hi! I am currently using a string to hold data (can be strings as well as binary!). Now it occured to me, that the <string> might be unable to handle Null-Bytes. Is that so? Following...
1
7277
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
1
2669
by: jithoosin | last post by:
Hi I am using STL in my code. So after some time it appears as if the code is leaking memory. I did a memory profiling using valgrind massif to find out that most memory in my code is allocated...
4
5662
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.