Getting started with the STL for the first time was awkward for me. Once I mastered the basics, using other templates became easier. The first template I used was vector. The second was bitset. Understanding how one template work, will help with using the other templates.
Note: The following code was not checked to see if it compiles.
Doing a basic example from main.cpp is pretty straight forward. Its when you want to use in classes for the first time while mastering C++ Object Oriented Programming that it took a couple of tries.
main.cpp
===========
//Note the newer style names do not include .h for C++
// You will still need the .h calls for some of the Netburner stuff.
// For information on bitset:
//
http://www.cplusplus.com/reference/stl/bitset/bitset/
#include <bitset>
// Including the C++ print methods. You could use #include <stdio.h> or <cstdio>, newer // style, but I recommend just jumping in. Note, for Netburner iprintf you will need the #include <stdio.h>
#include <iostream>
// The entire legacy C++ method calls are now wrapped in there own namespace.
// You will either make calls like std::cout, std::endl, std::bitset. To avoid having
// to do this. The following will set the namespace. You will need to do it for each // file you use the c++ calls in. If you have a limited number of calls per page, // // then std:: should suffice.
use namespace std;
// We need a constant for our bitset declaration.
// We are going to create a bitset that using 1 BYTE/8bits per item.
// The advantage of bitset over char is we do not have to use the basic
// bitwise operators for flipping bits, etc. For more complicated bit manipulations
// bitwise operators may be needed, but those operators are available through
// bitset.
enum {
BITSET_SIZE = 8
};
void main(void)
{
//Get in the habit of not using numerical constants in your declaration
bitset<BITSET_SIZE> mybits;
// In this example we could initialize the mybits in the above declaration, but
// When you start making classes, this tends to become a problem.
mybits.set(2); // 00000100
}
In C++ classes:
=======================
mybits.h
#include <bitset>
using namespace std;
enum {
BITSET_SIZE = 8
}
Class MyClassBits {
bitset<BITSET_SIZE> mybits;
}
MyClassBits.cpp
// Good practice is to list the #include you use in a file
// even if you inherit from the .h file.
#include <bitset>
#include <iostream>
//This is needed as it is not inherited from .h
using namespace std;
MyClassBits::MyClassBits ()
{
mybits.set(); // This will initialize to 11111111
public:
void someOtherMethod();
}
void MyClassBits::someOtherMethod()
{
// The interested thing about printing bitset is we get the
// 10101011 pattern to see how the value would look.
// This came in handy for working with a PCA9506 IO expander chip
// where I need to compare the bitset to what was being sent to the chip.
// If you are using LED the lit LED's are 1's and the off LED are 0's.
cout << "My Bits: " << mybits << endl;
// What if I need to print the integer value?
// Use mybits.to_ulong()
cout << mybits << " as an integer is: " << mybits.to_ulong() << endl;
}