草庐IT

fixed_string

全部标签

c++ - c++ 中 std::fixed、std::scientific 等的正确类型是什么?

我正在尝试将std::fixed、std::scientific等放入变量中,但不知道如何操作。我正在尝试这段代码,但它不起作用:typedefstd::vectorFlagArray;intmain(){FlagArraytmp1={std::fixed,std::scientific};FlagArraytmp2={std::internal,std::right,std::left};FlagArraytmp3={std::uppercase,std::showbase,std::showpoint,std::showpos};return0;} 最佳

C++ 从 char* 到 char 的无效转换 (char* = *string.begin() )

我有以下代码:std::stringextract(){fstreamopenfile("/home/name/Documents/testfile");std::stringteststring;longlocation=4;longlength=2;teststring.resize(length);char*begin=*teststring.begin();openfile.seekp(location);openfile.read(begin,length);returnteststring;}此代码应该返回在文件中找到的字符串。例如,如果文件的内容是StackOverflo

c++ - "Why switch statement cannot be applied on strings?"的答案是否仍然正确,即使使用 C++11/14?

我遇到了这个问题:Whyswitchstatementcannotbeappliedonstrings?并想知道答案是否:Thereasonwhyhastodowiththetypesystem.C/C++doesn'treallysupportstringsasatype.Itdoessupporttheideaofaconstantchararraybutitdoesn'treallyfullyunderstandthenotionofastring.仍然适用,即使在C++11/14中使用std:string。是否有多个elseif(...)的替代方案?

c++ - boost::shared_ptr<string> 标准集

我有一组boost::shared_ptr,我希望它不是通过共享指针而是通过字符串来排序和唯一化的。我是否必须提供一个新的比较函数来获取共享指针并比较内容,或者已经存在我可以使用的比较器? 最佳答案 这非常具体,因此您可能需要一个自定义比较器。这应该有效:structpointercompare{booloperator()(constboost::shared_ptr&a,constboost::shared_ptr&b){return(*a)>(*b);}} 关于c++-boost:

c++ - 插入以设置为 "Fixed"大小循环

所以我想将我的set中所有元素的两倍插入回set中。但显然我需要获取结束迭代器,这样我就不会一直迭代新元素。(别担心,我检查过,set::insert不会使迭代器无效:http://www.cplusplus.com/reference/set/set/insert/#validity)所以给定输入setfoo={1,2,3},这就是我所做的:for(autoit=begin(foo),finish=end(foo);it!=finish;++it){foo.insert(*it*2);}我希望我的集合包含:1,2,3,4,6惊喜!它包含:-2147483648,-1073741824

c++ - 为什么 c++ string == (equality) 运算符比手动逐个检查字符快得多?

我在玩C++字符串,发现使用C++字符串==运算符比手动逐个检查字符要快得多:#include#include#includeusingnamespacestd;//assumess1ands2areofsamelengthboolmyEqual(string&s1,string&s2){inti=0;intj=0;while(i输出显示:MyEqual:18==operator:3对于较大的字符串,差异更为显着。我最初认为c++string==operator会做一些与手动逐个比较字符非常相似的事情,但显然它使用了一些优化来显着优于手动方法。c++string==操作符做了哪些优化?

c++ - 从相同的硬编码字符串文字初始化 std::string 和 std::wstring

我正在编写一些单元测试时偶然发现了一个已经成功困扰我几次的场景。我需要生成一些字符串来测试JSON编写器对象。由于作者同时支持UTF16和UTF8输入,所以我想用这两种输入进行测试。考虑以下测试:classUTF8;classUTF16;templatevoidwriteJson(std::map&data){//Writetofile}voidgenerateStringData(std::map&data){data.emplace("Lorem","LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.");

c++ - 我对 std::fixed 的使用缺少什么?

我正在编写一个程序来模拟plinko板,在该程序中我有一个函数可以输出分配给每个插槽的钱。我正在尝试使用std::fixed和std::setprecision输出每个值,小数点后有两个零,但每个值的输出就像我根本没有使用fixed一样。我错过了什么?这是我的代码:#include#includeusingnamespacestd;voidChipsAllSlots(intnumChips){constintNUM_SLOTS=9;constintslotWinnings[NUM_SLOTS]={100,500,1000,0,10000,0,1000,500,100};for(inti

c++ - char[](c-string) 的初始化标准

考虑以下代码:intmain(){charhi[5]="Hi!";printf("%d",hi[4]);}将打印什么?更重要的是,在最新C++标准中,标准中是否提及将打印的内容?如果有提及,它在哪里?由于某种原因,很难找到关于此的最新信息,而且各种来源的信息相互矛盾。我试过en.cppreference.com和cplusplus.com,前者没有任何信息,后者声称值未确定。然而,usingnamespacestd;intmain(){charmySt[1000000]="Hi!";for(inti=0;i这在我的系统上只打印0、1和2,所以我希望“rest”被初始化为“\0”。另外据

C++ 自动类型转换为 std::string 和 char* 的区别

作为学习练习,我一直在研究C++中的自动类型转换是如何工作的。我知道通常应该避免自动类型转换,但我还是想通过了解它的工作原理来增加我对C++的了解。我已经创建了一个可以自动转换为std::string的StdStringConverter类,但是编译器(Debian上的g++4.3.4)似乎没有这样做将对象与真实的std::string进行比较时的转换(请忽略缺少按引用传递和不必要地创建临时对象的情况):#includeclassStdStringConverter{public:explicitStdStringConverter(std::stringname):m_name(na