我想编写一个方法,它将采用一个整数并返回一个std::string用逗号格式化的整数.
示例声明:
std::string FormatWithCommas(long value);
Run Code Online (Sandbox Code Playgroud)
用法示例:
std::string result = FormatWithCommas(7800);
std::string result2 = FormatWithCommas(5100100);
std::string result3 = FormatWithCommas(201234567890);
// result = "7,800"
// result2 = "5,100,100"
// result3 = "201,234,567,890"
Run Code Online (Sandbox Code Playgroud)
将数字格式化为string逗号的C++方式是什么?
(奖金也将用于处理double.)
我正在尝试实现一些STL样式的排序算法.std::sort看起来像这样的原型(来自cplusplus.com):
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );
Run Code Online (Sandbox Code Playgroud)
该函数通常被称为这样(虽然容器类型可以变化):
std::vector<int> myVec;
// Populate myVec
std::sort(myVec.begin(), myVec.end());
Run Code Online (Sandbox Code Playgroud)
我复制了std::sort我自己的排序功能的原型.要遍历要排序的容器,我执行以下操作:
template <class RandomAccessIterator>
void mySort(RandomAccessIterator first, RandomAccessIterator last) {
RandomAccessIterator iter;
for (iter = first; iter != last; ++iter) {
// Do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
很容易.但是如果我想使用反向迭代器呢?这在从两端对容器进行分类的算法中是方便的,例如鸡尾酒排序.
有没有办法从作为参数传入的迭代器中获取反向迭代器?如果我事先知道容器类型,我可以这样做:
template <class RandomAccessIterator>
void mySort(RandomAccessIterator first, RandomAccessIterator last) {
std::vector<int>::reverse_iterator riter(last);
std::vector<int>::reverse_iterator rend(first);
for ( ; riter != rend; ++riter) {
// Do stuff …Run Code Online (Sandbox Code Playgroud) 我知道如何从向量迭代器中获取索引,方法是从中减去begin迭代器.例如:
vector<int>::iterator it = find(vec.begin(), vec.end(), x);
size_t position = it - vec.begin();
Run Code Online (Sandbox Code Playgroud)
但是,现在我想找到x向量中最后一个的索引.如何从反向迭代器中获取实际索引?我发现以下似乎有效(编辑:它没有)但也许有更好的(更惯用或其他......)方式.
vector<int>::reverse_iterator it = find(vec.rbegin(), vec.rend(), x);
size_t position = vec.size() - (it - vec.rbegin());
Run Code Online (Sandbox Code Playgroud) 我想std::vector在for循环中迭代一些s,但是根据某些条件,向量应该向前或向后迭代.我想,我可以通过使用普通迭代器或反向迭代器来轻松实现,如下所示:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec{0, 1, 2, 3, 5, 6, 7};
bool reverse = true;
std::iterator<random_access_iterator_tag, int> it, end_it;
if (reverse) {
it = vec.rbegin();
end_it = vec.rend();
} else {
it = vec.begin();
end_it = vec.end();
}
for (; it != end_it; it++) {
cout << *it << ", ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是vector::begin(),vector::rbegin()似乎并没有使用相同的父类.是否有另一种方法可以做我想要的,而不在if-else结构中有两个不同的循环?当然我可以为循环体创建一个函数/ lambda或者使用一些索引算法但是有更优雅的方法吗?
编译器抱怨分配,it = vec.begin()因为它们是不同的类型.gcc和VC++输出不同的错误,似乎使用不同的类型作为返回值vector::begin.
这似乎是一个简单的问题,它肯定是可行的,但我想有效地做到这一点.
目标:
如果符合条件,则从std :: list中删除最后一个元素.
问题:
我的编译器(MSVC++ 10)不喜欢将反向迭代器转换为const迭代器,以便对std :: list.erase()进行方法调用.消息是:
Run Code Online (Sandbox Code Playgroud)error C2664: 'std::_List_iterator<_Mylist> std::list<_Ty>::erase(std::_List_const_iterator<_Mylist>)' : cannot convert parameter 1 from 'std::reverse_iterator<_RanIt>' to 'std::_List_const_iterator<_Mylist>'
我试过的代码:
std::list<mytype> mylist;
// lots of code omitted for clarity
bool ends_badly = true;
while(ends_badly && mylist.size() > 0)
{
auto pos = mylist.crbegin(); // Last element in the list
if ((*pos)->Type() == unwanted)
{
mylist.erase(pos); // Here is where the compiler complains
}
else
{
ends_badly = false;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过使用前向迭代器并循环遍历列表来解决这个问题,但这太麻烦了.在这个上下文中,编译器可以使用正向迭代器,我尝试将反向迭代器转换为const迭代器,但编译器也不喜欢它.
使用反向迭代器从双向列表中删除列表元素似乎是合理的.有什么明显的东西我在这里不见了吗?
我正在为我的程序编写一个小UI.我有方法onMouseMotion(),我可以用两种方式之一调用(参见代码); 如果我通过它std::function,那么!=for循环停止条件中的运算符会产生运行时异常vector iterators incompatible.为什么?
class Widget : public EventHandler
{
protected:
/* ... */
std::vector<Widget *> children_;
std::function<bool(Event &)> func_;
private:
bool onMouseMotion(Event &event);
/* ... */
};
Widget::Widget()
{
/* ... */
func_ = std::bind(&Widget::onMouseMotion, this, std::placeholders::_1);
/* ... */
}
bool Widget::processEvent(Event &event)
{
if (event.getType() == ui::EventType::MouseMotionEvent) {
/* Method 1 - onMouseMotion works ok */
onMouseMotion(event);
/* Method 2 - onMouseMotion throws */
//func_(event);
return true;
}
} …Run Code Online (Sandbox Code Playgroud)