I am new to the use of the boost library and have questions regarding its use. I have a vector union of ints and strings generated by use of boost::variant. Assume the vector is called myvec
. I tried following the advice on this post to extract elements of the vector; essentially myvec[i]
where i is the iterator integer in a for-loop
. However, I am getting errors when I try to access elements in this way. It was advised by Ari that boost::apply_visitor
is the best for this.
How can this error be fixed? In fixing this, would I have to use the same method if I had a conditional (say in an if
statement).
Furthur, how could I count the number of strings elements within the vector myvec
? This is because I would like to find the length of the vector excluding all of the string elments.
Finally, I would like to clean up my code and so I have functions doing seperate parts. I would like to pass a function called function1
as an argument to another function function2
. As I understand, the boost_bind
and boost_function
need to be used given the cumbersome alternative by ordinary C++ code. But how can this be done?
For each of the questions, culd you please provide an example code and list any headers or libraries that need to be linked. would I hav to, for example, link all of the command I use; -lboost_apply_visitor
, -lboost_bind
, -lboost_function
etc.
I am using C++ in Xcode 6.1. Thanks for all your help in advance.
EDIT: The code that I have is:
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/variant.hpp>
#include <boost/get.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
using namespace std;
typedef vector<int> int_vec;
typedef boost::variant<std::string,int> StringOrInt;
//typedef boost::function< void() > Function; // This allows for functions to be taken as arguments
// ------------------
// SET UP FIELD ARRAY
// ------------------
int *standard (int fieldarray[], int j, int n){
for (int i=0; i<n; i++){fieldarray[i] = n - i;}
for (int i=n; i<n+2; i++){fieldarray[i] = j;}
int k = 1;
for (int i=n+2; i< (2*n + 2); i++){fieldarray[i] = k; k++;}
return fieldarray;
}
// -----------------
// SET UP BOOL ARRAY
// -----------------
int *comparison (int boolarray[], int n){
for (int i=0; i<n; i++){boolarray[i] = 0;}
boolarray[n] = 1;
boolarray[n+1] = 0;
for (int i=n+2; i< (2*n + 2); i++){boolarray[i] = 1;}
return boolarray;
}
// ------------------------
// DELTA FUNCTION GENERATOR
// ------------------------
void deltafunction(vector<StringOrInt>&x, int i){
stringstream name;
name << "\u03b4" << "(" << x[i-1] << "-" << x[i] << ")";
cout << name.str() << endl;
}
// ----------------------------------------------------
// SWAPS VECTOR ELEMENTS ASSOCIATED WITH THE COMMUTATOR
// ----------------------------------------------------
void swap_element(vector<StringOrInt>&x, int i){
StringOrInt c = x[i];
x[i] = x[i-1];
x[i-1] = c;
}
// -----------------------
// START THE MAIN FUNCTION
// -----------------------
int main()
{
int n=4, j=3, x[2 * n + 2], y[2 * n + 2]; // x and y for the field and bool arrays
int *fieldarray = standard(x, j, n), *boolarray = comparison(y, n);
vector<StringOrInt> fields (fieldarray, fieldarray + (2 * n + 2)); // Field vector from field arrays
vector<StringOrInt> bools (boolarray, boolarray + (2 * n + 2));
vector< vector<StringOrInt> > normally_ordered_fields; // Empty vector of vectors
/*for (int j=bools.size() - 1; j >= 0; j--){
if (bools[j] == 1 && bools[j - 1] == 0){
vector<StringOrInt> reduced = bools;
swap_element(bools, j);
reduced.erase(reduced.begin() + j, reduced.begin() + j + 1);
stringstream name; // Declare the string
name << "\u03b4" << "(" << bools[j-1] << "-" << bools[j] << ")";
reduced.push_back(name.str());
j = -1;
}
}*/
return 0;
}
As can be seen, I am setting the vector to only have integer arguments at first. But then my some operations, there will be the introduction of a string (which is done by the deltafunction
).
For this to work, one needs to link the boost library. Also if we remove the /* */ braces from the for-statement
within the main, I get a complaint, since I am trying to access elements within the vector through its index.
I would like to promote the contents of the if statement within the for-loop to a function. But for it to work, it would require the swap_elements
function to be passed as an argument. How could this be done.
Also, whilst I am not calling the deltafunction
, how could I count the number of elements which have int arguments.