我最近想使用boost :: algorithm :: join但我找不到任何用法示例,我不想花很多时间学习Boost Range库只是为了使用这个函数.
任何人都可以提供一个如何在字符串容器上使用连接的好例子吗?谢谢.
我知道如何用其他语言来做这件事,而不是C++,我不得不在这里使用它.
我有一组字符串,我打印到列表中,并且每个字符串之间需要逗号,但不是逗号.例如,在java中,我会使用stringbuilder,并在构建字符串后删除逗号.我如何用C++做到这一点?
auto iter = keywords.begin();
for (iter; iter != keywords.end( ); iter++ )
{
out << *iter << ", ";
}
out << endl;
Run Code Online (Sandbox Code Playgroud)
我最初尝试插入此块来执行此操作(在此处移动逗号打印)
if (iter++ != keywords.end())
out << ", ";
iter--;
Run Code Online (Sandbox Code Playgroud)
我讨厌小东西把我绊倒的时候.
编辑:谢谢大家.这就是我在这里发布这样的东西的原因.这么多好的答案,并以不同的方式解决.经过一个学期的Java和汇编(不同的类),不得不在4天内完成一个C++项目,这让我陷入了困境.我不仅得到了答案,还有机会思考解决这类问题的不同方法.真棒.
我想用自定义分隔符将a的内容复制vector到一个long string.到目前为止,我已经尝试过:
// .h
string getLabeledPointsString(const string delimiter=",");
// .cpp
string Gesture::getLabeledPointsString(const string delimiter) {
vector<int> x = getLabeledPoints();
stringstream s;
copy(x.begin(),x.end(), ostream_iterator<int>(s,delimiter));
return s.str();
}
Run Code Online (Sandbox Code Playgroud)
但我明白了
no matching function for call to ‘std::ostream_iterator<int, char, std::char_traits<char> >::ostream_iterator(std::stringstream&, const std::string&)’
Run Code Online (Sandbox Code Playgroud)
我试过charT*但我得到了
error iso c++ forbids declaration of charT with no type
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用char,ostream_iterator<int>(s,&delimiter)
但我在字符串中得到奇怪的字符.
任何人都可以帮助我理解编译器在这里期待什么吗?
我想建立std::string一个std::vector<std::string>.
我可以使用std::stringsteam,但想象有一个更短的方式:
std::string string_from_vector(const std::vector<std::string> &pieces) {
std::stringstream ss;
for(std::vector<std::string>::const_iterator itr = pieces.begin();
itr != pieces.end();
++itr) {
ss << *itr;
}
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
我怎么可能这样做?
在审阅时,我有时会遇到这种循环:
i = begin
while ( i != end ) {
// ... do stuff
if ( i == end-1 (the one-but-last element) ) {
... do other stuff
}
increment i
}
Run Code Online (Sandbox Code Playgroud)
然后我问这个问题:你会写这个吗?
i = begin
mid = ( end - begin ) / 2 // (the middle element)
while ( i != end ) {
// ... do stuff
if ( i > mid ) {
... do other stuff
}
increment i
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这超过了编写循环的意图:你循环,因为每个元素都有一些共同点.使用此构造,对于某些元素,您可以执行不同的操作.因此,我总结说,您需要为这些元素单独的循环:
i = begin …Run Code Online (Sandbox Code Playgroud) language-agnostic loops for-loop control-structure while-loop
允许编写更高效的C++代码的C++ 0x改进之一是unique_ptr智能指针(太糟糕了,它不允许像memmove()那样移动:提案没有进入草案).
即将推出的标准中有哪些其他性能改进?以下代码为例:
vector<char *> v(10,"astring");
string concat = accumulate(v.begin(),v.end(), string(""));
Run Code Online (Sandbox Code Playgroud)
代码将连接vector v中包含的所有字符串.这段简洁代码的问题在于accumulate()复制周围的东西,并且不使用引用.并且每次调用string()时都会重新分配加上运算符.因此,与优化良好的模拟C代码相比,代码性能较差.
C++ 0x是否提供了解决问题的工具,也许还有其他工具?
基本上它应该列出这种格式的所有矢量坐标:
(x,y,z)
但此刻它确实喜欢这个(x,y,z,)
最简单的方法是在for循环中使用if,但是我可以从out变量中减去一小段字符串吗?
我的代码:
template <unsigned short m>
std::ostream& operator<<(std::ostream& out, const Vector<m>& v) {
out << "(";
for(int i = 0; i < m; i++) {
out << v.coords[i] << ", ";
}
out << ")";
return out;
}
Run Code Online (Sandbox Code Playgroud) 我有以下清单:
vector<string> mylist;
Run Code Online (Sandbox Code Playgroud)
我想创建一个新字符串,其中 mylist 中的所有字符串都用逗号分隔,多年来我一直在编写丑陋的代码来检查我是否在向量中的最后一个字符串中,以了解是否应该添加逗号。
代码看起来像这样:
for(unsigned int i=0;i<mylist.size(); i++)
{
outstring+=mylist[i];
if(i<mylist.size()-1) // Avoiding the last comma
{
outstring+=",";
}
}
Run Code Online (Sandbox Code Playgroud)
是否有避免if语句的技巧,也许是使用 STL 的一些算法/迭代器技巧?
谢谢
我正在完成Bjarne Stroustrup的书"使用C++的原理和实践"(第3章练习6)中的练习.您被要求制作一个接受用户输入的程序(3个整数值),并按照从最小到最大的顺序打印3个值.这是我的解决方案,
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Please enter 3 integers: ";
int num1, num2, num3;
while (cin >> num1 >> num2 >> num3) {
if (num1 < num2 && num2 < num3)
cout << num1 << ", " << num2 << ", " << num3 << endl;
if (num2 < num1 && num1 < num3)
cout << num2 << ", " << num1 << ", " << num3 << endl;
if (num3 …Run Code Online (Sandbox Code Playgroud) c++ ×8
stl ×3
string ×3
boost ×1
c++11 ×1
for-loop ×1
if-statement ×1
join ×1
loops ×1
optimization ×1
ostream ×1
pretty-print ×1
stdstring ×1
stringstream ×1
unique-ptr ×1
vector ×1
while-loop ×1