我有以下代码用于格式化打印输出,总是4位数,包含符号:
std::stringstream pitch;
pitch.precision(0);
pitch.width(4);
pitch.fill('0');
pitch << std::showpos << (int)(m_values["Pitch_1"]);
Run Code Online (Sandbox Code Playgroud)
我还想显示符号("+"/" - "),但我希望它在填充之前,如下所示:
+002
Run Code Online (Sandbox Code Playgroud)
但是,我在这里的代码将"+"符号移动到最高位:
00+2
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我如何更改格式以便我拥有前者而不是后者?
我有一个文本,格式为"key:value".实际文本可能看起来像是,"Server: nginx"或"Server:nginx"忽略键:和值之间的空格.
什么可能是最快和最短的方式将其分成一个std::pair<std::string, std::string>?
是std::to_string跨平台运营吗?它是否可以编译并适用于所有平台?如果不是,还有什么选择?
谢谢
我正在std::string使用此代码修剪一个long 以使其适合文本容器.
std::string AppDelegate::getTrimmedStringWithRange(std::string text, int range)
{
if (text.length() > range)
{
std::string str(text,0,range-3);
return str.append("...");
}
return text;
}
Run Code Online (Sandbox Code Playgroud)
但在其他语言的情况下,如HINDI "??????"长度std::string是错误的.
我的问题是如何在所有测试用例中检索std :: string的准确长度.
谢谢
我想以同样的方式处理特定类的所有孩子.
到目前为止,我一直在检查dynamic_cast这样:
if(dynamic_cast<ParentClass*>(child_object))
{
// handle the object
}
Run Code Online (Sandbox Code Playgroud)
在我不需要强制使用子对象的情况下,有没有更好的方法呢?
我的第一次尝试是:
if(std::is_base_of<ParentClass, typeid(child_object)>::value)
Run Code Online (Sandbox Code Playgroud)
这当然不能作为is_base_of期望的两个class参数和typeid()返回std::type_info.
所以我的问题是,这样做的正确方法是什么?或者dynamic_cast即使没有使用铸造的物体,它也是正确的设施吗?
这是我想要实现的具体例子.我正在迭代QGraphicsItem与我感兴趣的对象发生碰撞的所有对象.我只想处理这些对象中的一组而忽略其余的对象.那组对象有一个共同的父对象.那么再次使用的dynamic_cast方式,还是有更好的选择?
for(QGraphicsItem* i : collidingItems())
{
if(dynamic_cast<ParentClass*>(i))
{
// handle specific group of objects that
//are children of ParentClass
}
}
Run Code Online (Sandbox Code Playgroud) 我的问题很快.
C++标准库是否为引用实现了哈希函数,例如 std::hash<T&>
例如:
#Include <array>
class UnionFind final {
public:
explicit UnionFind(int numbers) {
// ???
}
private:
std::array<int, ???> array_;
};
Run Code Online (Sandbox Code Playgroud)
我想初始化array_为std::array<int, numbers>在构造函数UnionFind(int numbers),但我不知道该怎么做.
我想实现一个哈希表示例.所以为了这个目标,我创建了一个头文件,一个hash.cpp和main.cpp文件.在我的hash.cpp中,我试图运行一个虚拟哈希函数,该函数获取键值并转换为索引值.但是,每当我尝试根据该哈希类创建对象时,它会抛出一个错误(对'hash'的引用是不明确的).
这是我的main.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
hash hash_object;
int index;
index=hash_object.hash("patrickkluivert");
cout<<"index="<<index<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的hash.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int hash(string key){
int hash=0;
int index;
index=key.length();
return index;
}
Run Code Online (Sandbox Code Playgroud)
这是我的hash.h
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
public:
int Hash(string key); …Run Code Online (Sandbox Code Playgroud) 是否有一个std函数可以轻松地将类的指针向量复制到类的向量中,还是我必须手动迭代它们?
寻找具有最快/更少代码行的解决方案:).
避免复制而不泄漏内存的解决方案也受到欢迎!
我有一张地图:
std::map<std::string, MyDataContainer>
Run Code Online (Sandbox Code Playgroud)
在MyDataContainer一些class或struct(无所谓).现在我想创建一个新的数据容器.假设我想使用默认构造函数来实现它:
// This is valid, MyDataContainer doesn't need constructor arguments
MyDataConstructor example;
// The map definition
std::map<std::string, MyDataContainer> map;
std::string name("entry");
// This copies value of `example`
map[name] = example;
// Below, I want to create entry without copy:
std::string name2 = "nocopy"
// This is pseudo-syntax
map.createEmptyEntry(name2);
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?当我想在地图中初始化它时跳过创建辅助变量?是否有可能使用构造函数参数?