[英]C++ macro for string literal begin iterator and end iterator
I would like a macro like this 我想要一个像这样的宏
STR_ITERATORS("My string")
which produces 哪个产生
pBegin, pEnd
You could use this to search a vector for example... 您可以使用它来搜索矢量,例如......
std::vector<char> str1;
std::search(str1.begin(), str1.end(), STR_ITERATORS("Look for this"));
Note that search takes two pairs of iterators, and the macro provides the second pair. 请注意,搜索需要两对迭代器,而宏提供第二对迭代器。
The macro can be written twice - once for normal chars, once for wide chars. 宏可以写两次 - 一次用于普通字符,一次用于宽字符。 It doesn't have to solve it all in one.
它不必一个一个地解决它。
Note: I don't have the option to use the very latest compilers. 注意:我没有选择使用最新的编译器。 (That makes it a bit boring doesn't it? :-)
(这让它有点无聊不是吗?:-)
In C++11, making your own search functions for this is easy. 在C ++ 11中,为此创建自己的搜索功能很容易。
template<class iter1, class iter2>
iter1 search(iter1 stackbegin, iter1 stackend, iter2 needlebegin, iter2 needleend)
{return std::search(stackbegin, stackend, needlebegin, needleend);}
template<class iter1, class needlecontainer>
iter1 search(iter1 stackbegin, iter1 stackend, const needlecontainer& needle)
{return std::search(stackbegin, stackend, std::begin(needle), std::end(needle));}
template<class stackcontainer, class needlecontainer>
decltype(std::begin(std::declval<stackcontainer>()))
search(const stackcontainer& stack, const needlecontainer& needle)
{return std::search(std::begin(stack), std::end(stack), std::begin(needle), std::end(needle));}
template<class stackcontainer, class needlecontainer>
decltype(std::begin(std::declval<stackcontainer>()))
search(stackcontainer& stack, const needlecontainer& needle)
{return std::search(std::begin(stack), std::end(stack), std::begin(needle), std::end(needle));}
and use them: 并使用它们:
int main() {
std::vector<char> str1;
auto it = search(str1, "Look for this");
}
Proof of compilation: http://ideone.com/cO1rz (this version of gcc didn't have std::begin
and std::end
so I had to define them myself) and a C++03 version: http://ideone.com/gt1Do 编译证明: http : //ideone.com/cO1rz (这个版本的gcc没有
std::begin
和std::end
所以我必须自己定义它们)和C ++ 03版本: http:/ /ideone.com/gt1Do
If you really really really want a short macro, and you can guarantee that literals share the same space: 如果你真的真的想要一个短宏,你可以保证文字共享相同的空间:
template<int N>
const char* end(const char(&s)[N]) {return s+N;}
#define STR_ITERATORS(X) X, end(X) //BE WARNED, HIGHLY UNSAFE, NOT PORTABLE
boost range provides overloads of algorithms to use ranges, and will automatically handle most STL-like collections. boost范围提供算法的重载以使用范围,并将自动处理大多数类似STL的集合。 Specifically, strings can automatically convert to ranges as well.
具体来说,字符串也可以自动转换为范围。 There are also the free functions begin and end.
还有免费功能的开始和结束。
template<int N>
int GetLengthOfLiteral(const wchar_t (&literal)[N]) { return N - 1; }
template<int N>
int GetLengthOfLiteral(const char (&literal)[N]) { return N - 1; }
#define LITERAL_ITERATORS(literal) (literal), ((literal) + GetLengthOfLiteral(literal)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.