在C++缓冲区中读取整个文件内容的好方法是什么?
虽然在普通CI中可以使用fopen(), fseek(), fread()函数组合并将整个文件读取到缓冲区,但对C++使用它仍然是个好主意吗?如果是,那么我怎样才能在打开时使用RAII方法,为缓冲区分配内存,读取和读取文件内容到缓冲区.
我应该为缓冲区创建一些包装类,它在它的析构函数中释放内存(分配给缓冲区),以及用于文件处理的相同包装器吗?
我想为std :: sort()创建自定义比较函数,以对一些键值对std :: pair进行排序
这是我的功能
template <typename K, typename V>
int comparePairs(const void* left, const void* right){
if((((pair<K,V>*)left)->first) <= (((pair<K,V>*)right)->first))
return 1;
else
return -1;
}
Run Code Online (Sandbox Code Playgroud)
然后,在一些类里面我有对类成员的向量:
vector<pair<K,V>> items;
Run Code Online (Sandbox Code Playgroud)
以及使用std :: sort()按键对此向量进行排序的一些方法
std::sort(items.begin(), items.end(), comparePairs<K,V>);
Run Code Online (Sandbox Code Playgroud)
我内部有编译错误,其中说
"无法将参数编号从'std :: pair <_Ty1,_Ty2>'转换为'const void*'"
.什么是错误?
可能重复:
在C++中的类初始值设定项中初始化const数组
这是一个新手问题.如何初始化常量整数数组类成员?我认为在相同的情况下经典数组不是最佳选择,我应该使用什么而不是它?
class GameInstance{
enum Signs{
NUM_SIGNS = 3;
};
const int gameRulesTable[NUM_SIGNS][NUM_SIGNS]; // how to init it?
public:
explicit GameInstance():gameRulesTable(){};
};
Run Code Online (Sandbox Code Playgroud) 我有一些私有类成员,表示std::deque包含一些数据的随机访问数组:
std::vector<std::deque<SomeDataClass> > someMember;
Run Code Online (Sandbox Code Playgroud)
我想提供一个公共类方法,它返回可迭代的数据结构,包含我的deques数组中的所有数据元素:
std::deque<SomeDataClass> someMethod();
Run Code Online (Sandbox Code Playgroud)
我希望这个方法遍历vector中的所有deques并复制它上面的每个元素到本地std :: deque,最终按值返回本地std :: deque.我想用C++ 11实现这个方法auto和std::begin(),std::end():
std::deque<SomeDataClass> MyClassName::someMethod(){
std::deque<DirectedEdge> allDataItems;
std::deque<DirectedEdge>::iterator deqIter = allDataItems.begin();
for(auto it = std::begin(someMember); it != std::end(someMember); ++it){
std::copy(std::begin(*it), std::end(*it), deqIter);
}
return allDataItems;
}
Run Code Online (Sandbox Code Playgroud)
我在deque标头中的运行时收到数据访问冲突未处理的异常错误.什么是错误?
我有一个继承两个接口的类:
class Multi : public IFoo, public IBar {
public:
virtual ~Multi();
// Foo part
virtual void fooMethod();
// ...
// Bar part
virtual void barMethod();
// ...
};
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个类不能在每个接口的两个单独的类中分解.事实上,在类实现中,这些实体(Foo和Bar)紧密耦合,但将来它们可能会分开.
另一个类想要使用Multi类,有一个指向IFoo和IBar的指针:
class ClientClass {
public:
ClientClass(); // constructor
// smth
private:
std::shared_ptr<IFoo> foo_;
std::shared_ptr<IBar> bar_;
};
Run Code Online (Sandbox Code Playgroud)
在构造函数中,我做了类似的事情:
ClientClass::ClientClass(){
auto pMulti = new Multi;
foo_ = std::shared_ptr<IFoo>(pMulti);
bar_= std::shared_ptr<IBar>(pMulti);
}
Run Code Online (Sandbox Code Playgroud)
但是每个共享指针都有单独的引用计数器,它会导致在类破坏时删除已经删除的指针,对不对?
std::system_error处理带有相关错误代码的异常.是否可以使用公共catch块来获取std :: system_error异常消息及其代码?像这样
try{
// code generating exception
} catch (const std::exception& ex){ // catch all std::exception based exceptions
logger.log() << ex.what(); // get message and error code
// if exception type is system_error
}
Run Code Online (Sandbox Code Playgroud)
唯一的方法是直接捕获std::system_error类型并在基类异常类型捕获之前获取其代码吗?广泛使用std :: system_error的最佳方法是什么?
我有一些非托管的 C++ 动态库和 C# GUI 应用程序,正在使用它。我想通过一些库提供的方法中的参数传递回调。是否可以将回调传递给 C# 中的非托管 C++ 方法。
// unmanaged C++
typedef uint8_t (__stdcall *SomeCallback)();
MYDLL_API uint8_t someMethod(SomeCallback cb);
Run Code Online (Sandbox Code Playgroud)
我正在尝试以这种方式使用库:
// C# part
public delegate Byte SomeDelegate();
[DllImport("mylibraryname.dll")]
public static extern Byte someMethod(ref SomeDelegate pDelegate);
// actuak callback
Byte myCallback() {
// some code
}
...
// call unmanaged passing callback
static void Main(string[] args) {
someMethod(myCallback);
}
Run Code Online (Sandbox Code Playgroud)
我在编译时收到错误:
cannot convert from 'method group' to 'ref SomeDelegate
Run Code Online (Sandbox Code Playgroud)
我的方法完全错误吗?
哈希图通常使用存储桶的内部数组(表)来实现。在通过键访问哈希图时,我们使用键类型特定(逻辑类型特定)哈希函数获取键的哈希码。然后我们需要将哈希码映射到实际的内部存储桶表索引。
key -> (hash function) -> hashcode -> (???) -> index in internal table
Run Code Online (Sandbox Code Playgroud)
有时,内部表可能会收缩和扩展,具体取决于哈希图填充率。那么 hashcode->index 转换方法可能可以稍微改变一下。
例如我们的哈希函数返回 32 位无符号整数值并且
A时刻:内表容量为10000
B时刻:内表容量为100000
通常使用什么算法或方法来进行hashcode->内表索引转换?如何为他们解决表大小调整问题?
我正在为 Windows 7 开发某种命名空间扩展。我的扩展在所有文件和文件夹的上下文菜单中提供了一些特定于应用程序的内容。但我想以不同的方式处理文件和文件夹。有没有办法判断IShellItem提供的接口对象是文件夹还是文件?
我有一些用于文件夹视图的Windows命名空间shell扩展.我想用它来与具有特定文件扩展名的文件进行用户交互.这些文件是由我的其他应用程序创建的.
如何在Windows注册表中正确注册它.我应该为我的扩展名和扩展名的CLSID或其他东西添加一些文件扩展名的密钥吗?
许多第三方C / C ++库提供多线程支持线程的优先级,相应的调度程序等。为什么现代C ++标准不支持此有用功能?
我有一些试图锁定的函数,std::mutex如果mutex成功锁定,函数std::thread使用lambda函数作为线程函数参数创建,并使用std::move()以下命令传递它:
static std::mutex mtx;
// some mutex defended stuff
void someFunc() {
// try lock mutex using unique_lock
std::unique_lock<std::mutex> lock(mtx, std::try_to_lock);
if(!lock.owns_lock()) {
return; // locking fails
}
// task for thread
auto task = [&](std::unique_lock<std::mutex>&& lock) {
// do async work and release lock when it done
lock.unlock();
// do something after releasing lock
};
// create thread, pass lock
std::thread taskThread(task, std::move(lock));
taskThread.detach();
}
Run Code Online (Sandbox Code Playgroud)
我有编译器错误:
<lambda_1918cc58d906c210588b1a8bb33f1b0d>::operator
()(std::unique_lock<_Mutex> &&) const' : cannot convert …Run Code Online (Sandbox Code Playgroud) 如果我只使用unsigned三元运算符条件中的类型,这些unsigned变量会自动转换为signed类型吗?
简单的例子:
unsigned int prevTickSeconds = 48;
unsigned int currentTickSeconds = 5;
unsigned int secondsLeft = (currentTickSeconds - prevTickSeconds ) ?
currentTickSeconds - prevTickSeconds :
60 - prevTickSeconds + currentTickSeconds;
Run Code Online (Sandbox Code Playgroud)
(currentTickSeconds > prevTickSeconds)如本例所示,这种结构是否可以正常工作?是否会为三元运算符中的条件自动执行类型转换?
c++ ×11
c++11 ×4
concurrency ×2
explorer ×2
windows ×2
algorithm ×1
arrays ×1
c ×1
c# ×1
com ×1
containers ×1
exception ×1
hash ×1
hashmap ×1
inheritance ×1
lambda ×1
marshalling ×1
raii ×1
registry ×1
std ×1