STL-other containers

Source: Internet
Author: User
Tags bitset bitwise operators

In the C ++ language, there are some elements such as arrays, strings, streams, and bitsets that are not part of the standard STL, but are related to STL to some extent. The following is a brief introduction.

Array as STL container

We know that "dumb" pointers can be good as iterators because they support the required operators. This is not a trivial matter. It means that you can use the regular C ++ array as an STL container and use the element pointer as an iterator by yourself. Of course, arrays do not provide methods such as size (), empty (), insert (), and erase (), so they are not truly containers. However, the array does support the iterator through pointers. Let's take a look at the next small example.

 

 
# Include <vector> # include <iostream> using namespace STD; int main () {int arr [10]; // Normal C ++ arrayvector <int> VEC; // STL vector // initialize each element of they array to the value of its indexfor (INT I = 0; I <10; I ++) {arr [I] = I;} // Insert the contents of the array into the end of the vectorvec. insert (VEC. end (), arr, arr + 10); // print the contents of the vectorfor (I = 0; I <10; I ++) {cout <VEC [I] <";} return 0 ;}

 TIPS:Note that the iterator indicating the first element of the array is only the address of the first element. The iterator that indicates the last position must be beyond the position of the last element, so this is the address of the first element plus 10.

 


String as STL container

We can regard string as a sequential container of characters. It includes the begin () and end () methods (will return the iterator pointing to the string), insert () and erase () methods, size (), empty (), and the basic functions of all other sequential containers. This is quite similar to vector, and even provides the reserve () and capacity () methods. However, unlike vector, string does not need to store elements continuously in the memory. In addition, some methods provided by vector, such as push_back (), are not provided (). For example:

 

 
# Include <string> # include <iostream> using namespace STD; int main () {string STR; Str. insert (Str. end (), 'H'); Str. insert (Str. end (), 'E'); Str. insert (Str. end (), 'L'); Str. insert (Str. end (), 'L'); Str. insert (Str. end (), '0'); For (string: const_iterator it = Str. begin (); it! = Str. End (); It ++) {cout <* it;} cout <Endl; return 0 ;}

 

Stream as STL container

Traditionally, input and output streams are not containers and they do not store elements. However, the stream can be considered as an element sequence, so it has some common features with STL containers. The C ++ Stream does not directly provide any STL-related methods, but the STL provides some special iterators named istream_iterator and outstream_iterator, so that it can "iterate" the input and output streams. With these iterators, you can adapt the input and output streams so that the input and output streams can be stored in various STLAlgorithmAre used as the source and target respectively. For example, you can use ostream_iterator to print the elements in a container using the copy () algorithm.Code:

 

 
# Include <algorithm> # include <iostream> # include <iterator> # include <vector> using namespace STD; int main () {vector <int> myvector; for (INT I = 0; I <10; I ++) myvector. push_back (I); // print the contents of the vectorcopy (myvector. begin (), myvector. end (), ostream_iterator <int> (cout, ""); cout <Endl; return 0 ;}

Ostream_iterator is a template class that uses elements as a type parameter. The constructor uses an output stream and a string as the parameter. This string is the string written to the stream after each element. (Because the string parameter is "" here, it is used to separate the elements of the output with spaces ).

 

Similarly, you can use the iterator abstraction to read values from an input stream using istream_iterator. Istream_iterator can be used as a source in algorithms and container methods. It is not as common as ostream_iterator. All input stream iterators are no longer provided here.

Bitset

Bitset is a set-length abstraction of bit sequences. One digit represents two values, usually 1 and 0, on and off, true and false. Bitset also uses set and unset ). You can use toggle or flip to change it from one value to another.

Bitset is not a real STL container. It is fixed-length, not templated to element types, and does not support iteration. However, this is a very useful tool that is often used with containers, so it is necessary to understand bitset.

Basic bitset knowledge

Bitset is defined in the <bitset> header file. It is templated based on the number of digits stored. The default constructor initializes all bitset bit fields to 0. Another constructor can create a bitset from a string consisting of 0 and 1.

You can use the Set (), reset (), and flip () Methods to adjust the value of a single bit. You can use an overloaded operator [] to access and set bitset bit fields. Note that calling operator [] on a non-const object will return a proxy object. You can assign a Boolean value to it, call Flip (), or use ~ . You can also use the test () method to access a single bit domain.

In addition, you can use regular stream insertion and extraction operators to complete stream input and output of bitset. Bitset is used as a string that contains 0 and 1.

The following is a small example.

 
# Include <bitset> # include <iostream> using namespace STD; int main () {bitset <10> mybitset; mybitset. set (3); mybitset. set (6); mybitset [8] = true; mybitset [9] = mybitset [3]; If (mybitset. test (3) {cout <"bit 3 is set! "<Endl ;}cout <mybitset <Endl; return 0 ;}

 

 

Output:

Tips: The leftmost character in the output string is the highest digit (that is, the number or index is 9 ).

Bitwise operators

In addition to basic bit processing routines, bitset also provides all the implementations of operators: &, |, ^ ,~ , <<,>>, <=, |=, ^=, <=And >=. These operators act like operations on the "real" bit sequence. The following is an example.

 

 
# Include <bitset> # include <iostream> using namespace STD; int main () {string str1 = "0011001100"; string str2 = "0000111100 "; bitset <10> bitsone (str1), bitstwo (str2); bitset <10> bitsthree = bitsone & bitstwo; cout <bitsthree <Endl; bitsthree <= 4; cout <bitsthree <Endl; return 0 ;}

Output result:

 

No.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.