2011-06-01 69 views
4

我首先在VS2010中用Microsoft VC++开始C++。我最近发现了一些工作,但我一直在使用RHEL 5和GCC。我的代码大多是本机C++,但我注意到一件事...海湾合作委员会的元组模板

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

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

这里是我的代码:

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

反正有从编译器不兼容救我的代码?

+3

的''类型是即将修订的C部分使用升压库版本++标准,如果您尝试将语言更改为C++ 0x,则可能在g ++中受支持。我不确定这是否会起作用,但这可能是问题的原因。 – templatetypedef 2011-06-02 00:02:55

+3

换句话说,试试'g ++ -std = C++ 0x' – Nemo 2011-06-02 00:13:46

+0

@Nemo明天我会试试,但现在,我很乐意使用对(按照答案中的建议)。谢谢。 – sj755 2011-06-02 00:24:22

回答

1

只要它只是一对可以使用

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

但我不认为元组标准C++(原来它被包含在TR1,因此也是标准的C++ 0x)。像往常一样,Boost虽然覆盖了你。所以包括:

#include "boost/tuple/tuple.hpp" 

将解决你的问题跨编译器。

+0

当然,我一直在使用元组,所以我忘记了一对中可以包含两个元素。 – sj755 2011-06-02 00:23:09

1

的编译器附带的TR1库还要在这里

#include <tr1/tuple.hpp> 

//... 

std::tr1::tuple<int, int> mytuple; 

当然对于便携性,你可以在此期间

相关问题