在C ++

时间:2016-03-25 14:25:19

标签: c++ boost boost-bind boost-variant boost-function

我是使用boost库的新手,对使用它有疑问。我有一个使用boost :: variant生成的int和字符串的向量联合。假设向量被称为myvec。我尝试按照post的建议来提取向量的元素;基本上myvec[i]其中 i for-loop中的迭代器整数。但是,当我尝试以这种方式访问​​元素时,我遇到了错误。 Ari建议boost::apply_visitor是最好的。

如何修复此错误?在修复此问题时,如果我有条件(例如在if语句中),我是否必须使用相同的方法。

更进一步,我如何计算向量myvec中的字符串元素数量?这是因为我想找到不包括所有字符串元素的向量的长度。

最后,我想清理我的代码,所以我有功能做单独的部分。我想将一个名为function1的函数作为参数传递给另一个函数function2。据我所知,考虑到普通C ++代码的繁琐替代方案,需要使用boost_bindboost_function。但是怎么做呢?

对于每个问题,请提供示例代码并列出需要链接的任何标头或库。例如,我可以链接我使用的所有命令; -lboost_apply_visitor-lboost_bind-lboost_function等。

我在Xcode 6.1中使用C ++。提前感谢你的帮助。

修改 我的代码是:

#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;
}

可以看出,我首先将向量设置为只有整数参数。但接下来是我的一些操作,会引入一个字符串(由deltafunction完成)。

为此,需要链接boost库。另外,如果我们从main中的for-statement中删除/ * * /大括号,我会收到投诉,因为我试图通过索引访问向量中的元素。

我想将for循环中的if语句的内容提升为函数。但要使其工作,需要将swap_elements函数作为参数传递。怎么可能这样做。

另外,虽然我没有调用deltafunction,但我如何计算具有int参数的元素数量。

0 个答案:

没有答案
相关问题