GCC中的元组模板 [英] Tuple templates in GCC

查看:155
本文介绍了GCC中的元组模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先在VS2010中用VC ++开发了C ++。我最近发现了一些工作,但我一直在使用RHEL 5和GCC。我的代码大多是本机C ++,但我注意到一件事... ...

GCC似乎没有认出< tuple> 头文件或元组模板。起初,我想也许这只是一个错字,直到我看着cplusplus.com,发现头部确实不是标准库的一部分。



问题是,我喜欢在Visual Studio中编写我的代码,因为环境比eclipse或netbeans环境要优越,美观,而且调试起来很轻松。事情是,我已经写了一大堆代码来使用元组,我非常喜欢我的代码。我应该如何处理这个问题?



以下是我的代码:

 使用std :: cout; 
使用std :: make_tuple;
使用std :: remove;
使用std :: string;
使用std :: stringstream;
使用std :: tolower;
使用std :: tuple;
使用std :: vector;

//定义三个条件来编码
enum {DONE,OK,EMPTY_LINE};
//包含条件和字符串向量的元组
typedef元组< int,vector< string>>码;


//将别名传递给字符串
//分析传递给它的行
Code ReadAndParse(string& line)
{

/ ******************************************* **** /
/ ****************移除评论**************** /
/ ******************************************* /
// Sentinel标记第一个
的位置//分号和索引位置本身
bool found = false;
size_t semicolonIndex = -1;

//将行转换为小写
for(int i = 0; i< line.length(); i ++)
{
line [i] = tolower(第[i]行);

//找到第一个分号
if(line [i] ==';'&&!found)
{
semicolonIndex = i;
//抛出标志
found = true;



//删除任何和分号以忽略注释
if(found!= false)
line.erase(semicolonIndex) ;


/ ************************************* ********** /
/ *****测试并看看有没有剩下的***** /
/ *********** ************************************ /

//要抓取和存储单词
代码;
字符串标记;
stringstream ss(line);
vector< string>话;

//一个标志确实表明我们是否有任何东西
bool emptyLine = true;

//虽然字符串流传递了任何
,而(ss>>令牌)
{
// //如果我们触及了这一点,一个单词
emptyLine = false;

//将它推到单词vector
words.push_back(token);
}

//如果我们所得到的只是一个空格,那么它是一个空行
if(emptyLine)
{
code = make_tuple(EMPTY_LINE,words );
返回码;
}


//此时它应该是好的
code = make_tuple(OK,words);
返回码;
}

有没有办法将我的代码从编译器兼容性中解脱出来?

解决方案

只要它只是一对,您可以使用

  typedef pair< int,vector< string>>码; 

但我不认为元组是标准的C ++(事实证明它包含在TR1中,标准C ++ 0x)。像往常一样,Boost虽然覆盖了你。所以包括:

$ $ p $ #includeboost / tuple / tuple.hpp

可以在编译器中解决您的问题。


I first started C++ with Microsoft VC++ in VS2010. I recently found some work, but I've been using RHEL 5 with GCC. My code is mostly native C++, but I've noticed one thing...

GCC doesn't appear to recognize the <tuple> header file, or the tuple template. At first I thought maybe it's just a typo, until I looked at cplusplus.com and found that the header is indeed not part of the standard library.

The problem is that I like to write my code in Visual Studio because the environment is way superior and aesthetically pleasing than eclipse or netbeans, and debugging is a breeze. The thing is, I've already written a good chunk of code to use tuples and I really like my code. How am I supposed to deal with this issue?

Here's my code:

using std::cout;
using std::make_tuple;
using std::remove;
using std::string;
using std::stringstream;
using std::tolower;
using std::tuple;
using std::vector;

// Define three conditions to code
enum {DONE, OK, EMPTY_LINE};
// Tuple containing a condition and a string vector
typedef tuple<int,vector<string>> Code;


// Passed an alias to a string
// Parses the line passed to it
Code ReadAndParse(string& line)
{

    /***********************************************/
    /****************REMOVE COMMENTS****************/
    /***********************************************/
    // Sentinel to flag down position of first
    // semicolon and the index position itself
    bool found = false;
    size_t semicolonIndex = -1;

    // Convert the line to lowercase
    for(int i = 0; i < line.length(); i++)
    {
        line[i] = tolower(line[i]);

        // Find first semicolon
        if(line[i] == ';' && !found)
        {
            semicolonIndex = i;
            // Throw the flag
            found = true;
        }
    }

    // Erase anything to and from semicolon to ignore comments
    if(found != false)
        line.erase(semicolonIndex);


    /***********************************************/
    /*****TEST AND SEE IF THERE'S ANYTHING LEFT*****/
    /***********************************************/

    // To snatch and store words
    Code code;
    string token;
    stringstream ss(line);
    vector<string> words;

    // A flag do indicate if we have anything
    bool emptyLine = true;

    // While the string stream is passing anything
    while(ss >> token)
    {
        // If we hit this point, we did find a word
        emptyLine = false;

        // Push it onto the words vector
        words.push_back(token);
    }

    // If all we got was nothing, it's an empty line
    if(emptyLine)
    {
        code = make_tuple(EMPTY_LINE, words);
        return code;
    }


    // At this point it should be fine
    code = make_tuple(OK, words);
    return code;
}

Is there anyway to save my code from compiler incompatibility?

解决方案

As long as it's just a pair you can use

typedef pair<int,vector<string>> Code;

But I don't think tuple is standard C++ (turns out it is included in TR1 and consequently also standard C++0x). As usual Boost has you covered though. So including:

#include "boost/tuple/tuple.hpp"

will solve your problem across compilers.

这篇关于GCC中的元组模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆