gpt4 book ai didi

c++ - GCC 中的元组模板

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:13:04 26 4
gpt4 key购买 nike

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

GCC 似乎无法识别 <tuple>头文件或元组模板。起初我以为这可能只是一个拼写错误,直到我查看 cplusplus.com 发现该 header 确实不是标准库的一部分。

问题是我喜欢在 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++ - GCC 中的元组模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6209067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com