我有一个叫做的类Action,它实际上是一个围绕Move对象deque的包装器.
因为我需要遍历Moves前向和后向的双端队列,所以我有一个前向迭代器和一个reverse_iterator作为类的成员变量.这样做的原因是因为当我前往或后退时,我需要知道何时离开了双端队的"终点".
这个类看起来像这样:
class Action
{
public:
SetMoves(std::deque<Move> & dmoves) { _moves = dmoves; }
void Advance();
bool Finished()
{
if( bForward )
return (currentfwd==_moves.end());
else
return (currentbck==_moves.rend());
}
private:
std::deque<Move> _moves;
std::deque<Move>::const_iterator currentfwd;
std::deque<Move>::const_reverse_iterator currentbck;
bool bForward;
};
Run Code Online (Sandbox Code Playgroud)
该Advance功能如下:
void Action::Advance
{
if( bForward)
currentfwd++;
else
currentbck++;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我希望能够检索当前Move对象的迭代器,而无需查询我是向前还是向后.这意味着一个函数返回一种类型的迭代器,但我有两种类型.
我应该忘记返回一个迭代器,并返回一个Move对象的const引用 吗?
最好的祝愿,
BeeBand
关于构成有效语言环境名称的内容,该标准几乎没有提及; 仅传递无效的区域设置名称导致std::runtime_error.哪些语言环境名称可用于常见的Windows编译器,如MSVC,MinGW和ICC?
我想使用fmt库格式化浮点数。
我尝试用小数点分隔符','格式化浮点数,并尝试此操作未成功:
#include <iostream>
#include <fmt/format.h>
#include <fmt/locale.h>
struct numpunct : std::numpunct<char> {
protected:
char do_decimal_point() const override
{
return ',';
}
};
int main(void) {
std::locale loc;
std::locale l(loc, new numpunct());
std::cout << fmt::format(l, "{0:f}", 1.234567);
}
Run Code Online (Sandbox Code Playgroud)
输出为1.234567。我想要1,234567
我浏览了fmt库的源代码,并认为小数点分隔符已硬编码为浮点数,并且不遵守当前语言环境。
给出以下代码:
cout << 1000;
Run Code Online (Sandbox Code Playgroud)
我想要以下输出:
1,000
Run Code Online (Sandbox Code Playgroud)
这可以使用std :: locale和cout.imbue()函数来完成,但我担心我可能会错过这里的一步.你能发现它吗?我正在复制当前的语言环境,并添加了一个千位分隔符,但逗号永远不会出现在我的输出中.
template<typename T> class ThousandsSeparator : public numpunct<T> {
public:
ThousandsSeparator(T Separator) : m_Separator(Separator) {}
protected:
T do_thousands_sep() const {
return m_Separator;
}
private:
T m_Separator;
}
main() {
cout.imbue(locale(cout.getloc(), new ThousandsSeparator<char>(',')));
cout << 1000;
}
Run Code Online (Sandbox Code Playgroud) 是否可以使用 fmt 格式化带有千位分隔符的数字?
例如这样的东西:
int count = 10000;
fmt::print("{:10}\n", count);
Run Code Online (Sandbox Code Playgroud)
我正在研究 fmt,所以我正在寻找仅适用于 fmt 库的解决方案,而不以任何方式修改语言环境。
我想到了这一点,因为在stackoverflow上提供的其他示例在C#中
string number_fmt(ulong n)
{
// cout << "(" << n << ")" << endl;
char s[128];
sprintf(s, "%lu", n);
string r(s);
reverse(r.begin(), r.end());
int space_inserted = 0;
size_t how_many_spaces = r.length() / 3;
if(r.length() % 3 != 0)
how_many_spaces += 1;
for(int i = 1; i < how_many_spaces; ++i)
{
r.insert(3 * i + space_inserted, " ");
space_inserted += 1;
}
reverse(r.begin(), r.end());
return r;
}
Run Code Online (Sandbox Code Playgroud)
您知道更好的解决方案吗?
给定一个数字12456789,我需要输出12,456,789没有太多编码.我可以使用C,C++或JavaScript中的任何内置函数吗?
我有一个函数,它接受一个双精度值并将其作为带有千位分隔符的字符串返回。你可以在这里看到它:c++:用逗号格式化数字?
#include <iomanip>
#include <locale>
template<class T>
std::string FormatWithCommas(T value)
{
std::stringstream ss;
ss.imbue(std::locale(""));
ss << std::fixed << value;
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
现在我希望能够将其格式化为带有美元符号的货币。具体来说,如果给定 20500 的两倍,我想得到一个字符串,例如“$20,500”。
在负数的情况下,前置美元符号不起作用,因为我需要“-$5,000”而不是“$-5,000”。
我正在使用Fedora上的Eclipse.我想用一个点来分隔数千个更容易理解的数字.这个数字是一个可以的整数值0<Value<99.999
所以...如果Value = 1000它显示1000和我想要的是显示1.000(注意点).我需要改变的代码是下一个:
char str[12];
sprintf(str, "%d", (int)(Value));
m_Text.SetText(str);
Run Code Online (Sandbox Code Playgroud)
而我的想法是做一些像:
if (Value > 999)
{
int RightPart = (int)Value % 1000;
int LeftPart = Val/1000 ;
}
Run Code Online (Sandbox Code Playgroud)
然后附加一个字符串LeftPart +"." + RightPart所以如果Value = 1563它1+.+563是1.563
我的问题是这样做非常难看,我想知道是否有更好的方法.我在谷歌搜索并发现imbue,locale但他们只是为了cout.我已经看到过一些职位,像这样和这个,但是这并不能帮助我与我的问题.
谢谢.
注意:我想说我不想改变输出格式.我想改变我收到的int,所以我可以在str var中得到点,稍后我将使用它.
注2:基本上代码必须:接收一个整数(Value),并将其像字符串一样发送到setText().setText()基本上会在屏幕上将它打印到何时何地,我希望它打印1.563而不是1563,这更难以阅读.
c++ ×9
fmt ×2
numbers ×2
string ×2
c ×1
currency ×1
dollar-sign ×1
format ×1
formatting ×1
int ×1
iterator ×1
javascript ×1
locale ×1
windows ×1