我在stackoverflow上找不到这个问题.但我想知道人们如何使用STL(没有花哨的提升)......只是一个时尚STL.技巧/提示/大多数情况下使用的案例多年来......也许还有很多......
让我们分享吧......
每个答案一个提示......代码示例 -
编辑是一个如此糟糕的问题,因为它导致downvotes?
小智 9
我最喜欢的是以下内容,可以将任何可流动的内容更改为字符串:
template <class TYPE> std::string Str( const TYPE & t ) {
std::ostringstream os;
os << t;
return os.str();
}
Run Code Online (Sandbox Code Playgroud)
然后:
string beast = Str( 666 );
Run Code Online (Sandbox Code Playgroud)
我在几乎所有的项目中使用STL,从循环(使用迭代器)到将输入分成程序.
用空格对输入字符串进行标记,并将结果输入到std :: vector中以便稍后解析:
std::stringstream iss(input);
std::vector<std::string> * _input = new std::vector<std::string>();
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter<std::vector<std::string> >(*_input));
Run Code Online (Sandbox Code Playgroud)
其他收藏夹当然是std :: reverse和其他各种算法<algorithm>.
我喜欢istream_iterator和ostream_iterator.
读取流并使其看起来像任何其他容器的简单方法:
// Copies a stream of integers on the std input
// into a vector.
int main()
{
std::vector<int> data;
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<>(),
std::back_inserter(data)
);
// By uisng the istream_iterator<> the input just becomes another container.
}
Run Code Online (Sandbox Code Playgroud)