C++ Tips, Part 4
C++ Tips, Part 4
Article 4
Aether Lee
Hachioji, Japan
Abstract
In the previous article, we used “vector” and its iterators along with algorithms to introduce
powerful features of C++. This time we will use another C++ container, “map”, to look
further into capabilities of C++.
1. Using “map”
#include <iostream>
#include <map>
intMAPint[1] = 10;
intMAPint[5] = 20;
intMAPint[17]= 30;
return 0;
}
The declaration of “intMAPint” can be read as “to map an integer to another integer” or
“to associate an integer value with an integer key”. The next 3 lines can be interpreted as
“associate 10 with the key 1, 20 with 5, and 30 with 17”. So, 3 pairs are constructed here.
Furthermore, as these lines imply, keys have to be unique in a map.
The for-loop in example 4-1 will output 18 lines on the screen, which list 15 “0”s in
addition to those 3 explicitly assigned values. Obviously, it is something unexpected, since
#include <iostream>
#include <map>
intMAPint[1] = 10;
intMAPint[5] = 20;
intMAPint[17]= 30;
return 0;
}
With iterators, only those created pairs will be printed on the screen. The way the
iterators are used in the for-loop also implies that keys in maps are ordered. In the last
example of this article, we will see that they are ordered in ascending order.
Before continuing on the usage of a map, an example of using bitset to simulate a linear
feedback shift register (LFSR) will be introduced here.
#include <iostream>
#include <bitset>
return 0;
}
This example is in fact a PRBS-7 generator. Each seed is correspondent to a code in the
bit stream. If the generation is invoked several times, an algorithm “generate” can help to
simplify the code appearance.
#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <iterator>
int main()
{
vector < bitset<7> > vCode;
vCode.resize(127);
bitset<7> seed(string("1010101"));
generate(vCode.begin(), vCode.end(), prbs(seed,7,6,1));
The algorithm “generate” calls the class “prbs” to generate objects to fulfill the vector.
When the class “prbs” is called for the first time, the private members of the class are
initialized with the values assigned. The generation in the do-while loop in example 4-3 is
composed in the operator(). So, when “generate” iterates, prbs() is repeatedly called and
then the next code is returned to the vector.
Consider now several polynomials are used for LFSRs; the generated bit streams have to
be saved along with taking polynomials as indexes.
#include <iostream>
#include <vector>
#include <map>
#include <bitset>
#include <algorithm>
#include <iterator>
int main()
{
map<string, vector< bitset<7> > > mapLFSR;
vector < bitset<7> > vCode;
vCode.resize(127);
bitset<7> seed(string("1010101"));
String type keys are used as indexes in the map “mapLFSR” and each key associates a
vector, which saves the whole bit stream generated by a polynomial. Each element of the
vector is a bitset object. As demonstrated above, a map provides a simple and convenient
way to index objects other than consecutive integers. Maps are hence often called
dictionaries. Also, to use maps is another way to build multi-dimensional arrays.
Last but not least, as introduced in the previous article, for_each is called to loop over all
pairs in the map, and fetch them to the sub function “cout_find”. In the function, copy is
used to print out the contents and find is used to find the occurrence of a specific code.
4. References
5. Copyright