How do I use a C++ TR1 library in visual c++ 2010?
3 回答
VS2010 comes with a few C++0x features built-in. Some features of TR1, such as the mathematical functions, are not included in the Visual C++ implementation of TR1.
boost has an implementation of TR1, you can get it by downloading boost.
To disable the C++0x/TR1 headers from VS2010 and use the boost implementation, define _HAS_CPP0X=0
in the project settings for your VS2010 project.
如果您想使用与 VS10 一起打包的 TR1 的实现,只需简单地 #include 所需的头文件并开始运行即可。并非所有 TR1 都包含在 TR1 的 VS10 实现中。您可以在此处找到 TR1 的哪些部分(以及整个 C++0x)包含在工厂提供的实现中的列表,这里是一个简单的示例,说明如何在 VS10 中使用正则表达式,取自MSDN 示例页面:
// std_tr1__regex__regex_search.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
int main()
{
const char *first = "abcd";
const char *last = first + strlen(first);
std::cmatch mr;
std::regex rx("abc");
std::regex_constants::match_flag_type fl =
std::regex_constants::match_default;
std::cout << "search(f, f+1, \"abc\") == " << std::boolalpha
<< regex_search(first, first + 1, rx, fl) << std::endl;
std::cout << "search(f, l, \"abc\") == " << std::boolalpha
<< regex_search(first, last, mr, rx) << std::endl;
std::cout << " matched: \"" << mr.str() << "\"" << std::endl;
std::cout << "search(\"a\", \"abc\") == " << std::boolalpha
<< regex_search("a", rx) << std::endl;
std::cout << "search(\"xabcd\", \"abc\") == " << std::boolalpha
<< regex_search("xabcd", mr, rx) << std::endl;
std::cout << " matched: \"" << mr.str() << "\"" << std::endl;
std::cout << "search(string, \"abc\") == " << std::boolalpha
<< regex_search(std::string("a"), rx) << std::endl;
std::string str("abcabc");
std::match_results<std::string::const_iterator> mr2;
std::cout << "search(string, \"abc\") == " << std::boolalpha
<< regex_search(str, mr2, rx) << std::endl;
std::cout << " matched: \"" << mr2.str() << "\"" << std::endl;
return (0);
}
与 GCC 不同,VC2010 中的 TR1 标头没有隔离在TR1/
目录中。我不是通过使用 VC 知道这一点,而是因为有人告诉我 GCC 的实现在这种方式下是不寻常的。
N1836 1.3/4:
建议使用默认未定义的宏来保护标准头文件中的附加声明,或者将所有扩展头文件(包括新头文件和具有非标准声明的标准头文件的并行版本)放在一个单独的目录中不是默认搜索路径的一部分。
因此,您可能还需要添加一个#define
. 不幸的是,他们没有将其标准化!