在一行中创建前缀序列

发布于 2024-08-21 13:26:37 字数 328 浏览 4 评论 0原文

给定初始化变量 unsigned aunsigned bb > astd::vector;大小为 ba 的字符串。如何用元素填充字符串,例如“x3”“x4”“x5”“x6”(如果a=3和< code>b=7) 对于任意 ab 使用一个 C++ 命令(意味着一个分号:))?

Given the initialized variables unsigned a, unsigned b with b > a and std::vector<std::string> strings of size b-a. How can I fill strings with the elements e.g. "x3" "x4" "x5" "x6" (in case a=3 and b=7) for arbitrary a and b with one C++ command (meaning one semicolon at all :))?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笨笨の傻瓜 2024-08-28 13:26:37

这是多么大的挑战啊!

while (a < b) strings.push_back('x' + boost::lexical_cast<std::string>(a++));

另外,将冗长的内容与曼努埃尔的答案进行比较:)

What a challenge!

while (a < b) strings.push_back('x' + boost::lexical_cast<std::string>(a++));

Also, compare verbosity with Manuel's answer :)

前事休说 2024-08-28 13:26:37

#define IM_NOT_A_SEMICOLON_REALLY ; 然后随意继续。

#define IM_NOT_A_SEMICOLON_REALLY ; then proceed at will.

百思不得你姐 2024-08-28 13:26:37

不太有挑战性...

std::transform(
    boost::make_counting_iterator(a), boost::make_counting_iterator(b), 
    strings.begin(), 
    "x" + boost::lambda::bind(boost::lexical_cast<std::string, unsigned int>, 
                              boost::lambda::_1));

Not too challenging...

std::transform(
    boost::make_counting_iterator(a), boost::make_counting_iterator(b), 
    strings.begin(), 
    "x" + boost::lambda::bind(boost::lexical_cast<std::string, unsigned int>, 
                              boost::lambda::_1));
高跟鞋的旋律 2024-08-28 13:26:37

UncleBen 答案的衍生版本,但仅使用 STL

while( a < b ) vStrings.push_back( 'x' + ( (std::stringstream&)( std::stringstream() << a++ ) ).str() );

a derivate of UncleBen's answer but using only the STL

while( a < b ) vStrings.push_back( 'x' + ( (std::stringstream&)( std::stringstream() << a++ ) ).str() );
静待花开 2024-08-28 13:26:37
BOOST_FOREACH(std::string & str, strings) str = "x" + boost::lexical_cast<std::string>(a++);
BOOST_FOREACH(std::string & str, strings) str = "x" + boost::lexical_cast<std::string>(a++);
不知在何时 2024-08-28 13:26:37

滥用逗号运算符,这显然不是分号:

while (a<b) {
   char s[12],
        t = (snprintf(s, 11, "x%d", a++), strings.push_back(s), 0);
}

Abusing comma operators, which are obviously not semicolons:

while (a<b) {
   char s[12],
        t = (snprintf(s, 11, "x%d", a++), strings.push_back(s), 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文