Is there any c++ class that can be used like a string. Which has all stuff needed like comparators and etc? I want to have something like string class that works on array of bytes instead of chars. I'm just asking because I don't want to write again something that already exists. I will use this class in std::map and etc.
Asked
Active
Viewed 1,478 times
1
-
`std::vector` has comparators. What is "etc"? – Benjamin Lindley May 15 '14 at 05:14
-
1[`sizeof(char)` is `1` (it is one byte in size)](http://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1) – Sveltely May 15 '14 at 05:16
-
@BenjaminLindley etc is other std templates. But mostly I will use map. I wonder if it is efficient to store it in vector? Isn't there any specific class for this case? – nosbor May 15 '14 at 09:03
-
1I was referring to the etc in "comparators and etc", from the second sentence. In other words, what other operations are you interested in besides comparators? Because comparators are covered. And yes, a vector is just as efficient as a string. They are both contiguous sequence containers. – Benjamin Lindley May 15 '14 at 09:07
2 Answers
2
That's exactly what an std::string
is. A char
is essentially a byte
. It takes up one byte of space and it accepts all logical and bitwise operators (bit shifting: <<
, >>
; logical comparisons: &
, |
; etc.).
If for some reason you need something like an std::string
but for a different datatype, simply use std::basic_string<DATATYPE>
. In the STL, string
itself is a typedef
for basic_string<char>
.

Daniel
- 1,920
- 4
- 17
- 35
-
But I suppose basic_string use strlen and many other functions related to char*. Many of those functions use '\0' as the end of array. But in my case when I use byte as a type then '\0' can be somewhere in the middle of array and don't have to be at the end. – nosbor May 15 '14 at 08:56
-
@nosbor No, basic_string does not use `strlen()` or other c-string functions. `basic_string
` is known as a C++ string; it does not need a null-terminator and you can put as many null bytes in it wherever you like. – Daniel May 15 '14 at 15:23
0
There is no such thing as byte
in c++
. You can use std::vector
with unsigned char
which has similar effect as byte in Java
for example.
typedef unsigned char BYTE;
typedef std::vector<BYTE> ByteString;

Rakib
- 7,435
- 7
- 29
- 45
-
I'm not able to check it now. Is there opportunity to use it as std::map key type? I mean std::map
var; Will it work efficiently? – nosbor May 15 '14 at 08:59 -
This is same as `std::string` with `unsigned char`, you can use it and I guess the efficiency will be equivalent to `string` version, but will mostly depend on how you use it. – Rakib May 15 '14 at 09:04
-
An `std::vector` is really not what's needed here, and it is definitely not the same as `std::string`. There are [several reasons a `basic_string<>` is better](http://stackoverflow.com/a/4557156/1313439), not to mention it's what the OP asked for. – Daniel May 15 '14 at 15:32