我首先在 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;
}
有没有办法让我的代码免受编译器不兼容的影响?
只要是一对就可以使用
typedef pair<int,vector<string>> Code;
但我不认为 tuple 是标准 C++(事实证明它包含在 TR1 中,因此也是标准 C++0x)。和往常一样,Boost 可以满足您的需求。所以包括:
#include "boost/tuple/tuple.hpp"
将解决您跨编译器的问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)