c – GCC中的元组模板

我首先在VS2010中使用Microsoft VC启动了C.我最近找到了一些工作,但我一直在使用RHEL 5和GCC.我的代码主要是原生C,但我注意到一件事……

GCC似乎没有认识到<元组>头文件元组模板.起初我想也许这只是一个错字,直到我查看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;
}

反正有没有从编译器不兼容性中保存我的代码

解决方法

只要它只是一对你可以使用

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

但我不认为元组是标准C(结果是它包含在TR1中,因此也包括标准C 0x).像往常一样,Boost让你满意.所以包括

#include "boost/tuple/tuple.hpp"

将跨编译器解决您的问题.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...