这似乎是一个微不足道的问题,但我现在已经挂了几个小时(可能是Java太多了我的C++ braincells).
我创建了一个具有以下构造函数的类(即没有默认构造函数)
VACaptureSource::VACaptureSource( std::string inputType, std::string inputLocation ) {
if( type == "" || location == "" ) {
throw std::invalid_argument("Empty type or location in VACaptureSource()");
}
type = inputType;
location = inputLocation;
// Open the given media source using the appropriate OpenCV function.
if( type.compare("image") ) {
frame = cvLoadImage( location.c_str() );
if( !frame ) {
throw std::runtime_error("error opening file");
}
}
else {
throw std::invalid_argument("Unknown input type in VACaptureSource()");
}
Run Code Online (Sandbox Code Playgroud)
}
当我想创建一个实例时,我会使用
// Create input data object …Run Code Online (Sandbox Code Playgroud) 所以,我正在编写一个类的代码,这个类将进入一个将被其他人使用的库.该类将拦截并处理传入的消息(详细信息并不重要,但它使用的是activemq-cpp库).这个消费者类的概要是
class MessageConsumer {
...
public:
void runConsumer();
virtual void onMessage(const Message* message);
}
Run Code Online (Sandbox Code Playgroud)
其中,runConsumer()建立连接并开始监听和onMessage()接收消息时被调用.
我的问题是:使用此代码的人将各自拥有处理不同消息的方式.如何保持MessageConsumer通用但提供这种灵活性,同时保持代码简单?
两种选择:
MessageConsumer自己的新课onMessage()吗?MessageConsumer吗?您怎么看?哪个选项更好,为什么?
谢谢!
简而言之,我的问题是:如果您有class,则MyClass<T>如何更改类定义以支持您拥有的情况MyClass<T, Alloc>,类似于STL vector提供的方式。
我需要此功能来支持共享内存的分配器。具体来说,我正在尝试在共享内存中实现环形缓冲区。当前它具有以下ctor:
template<typename ItemType>
SharedMemoryBuffer<ItemType>::SharedMemoryBuffer( unsigned long capacity, std::string name )
Run Code Online (Sandbox Code Playgroud)
在哪里ItemType是要放置在缓冲区的每个插槽中的数据的类型。
现在,当我从主程序创建缓冲区时,效果如此出色
SharedMemoryBuffer<int>* sb;
sb = new SharedMemoryBuffer<int>(BUFFER_CAPACITY + 1, sharedMemoryName);
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,缓冲区本身不会在共享内存中创建,因此其他进程无法访问该缓冲区。我想做的是能够做类似的事情
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
typedef SharedMemoryBuffer<int, ShmemAllocator> MyBuffer;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShmemAllocator alloc_inst (segment.get_segment_manager());
MyBuffer *mybuf = segment.construct<MyBuffer>("MyBuffer")(alloc_inst);
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何向类模板添加显式分配器。
重复调用以vector::size()重新O(n)计算元素的数量(计算)或将此值存储在某处(O(1)查找).例如,在下面的代码中
// Split given string on whitespace
vector<string> split( const string& s )
{
vector<string> tokens;
string::size_type i, j;
i = 0;
while ( i != s.size() ) {
// ignore leading blanks
while ( isspace(s[i]) && i != s.size() ) {
i++;
}
// found a word, now find its end
j = i;
while ( !isspace(s[j]) && j != s.size() ) {
j++;
}
// if we found a word, add …Run Code Online (Sandbox Code Playgroud)我被问过这个问题一次,仍然无法弄清楚:
你有一个N整数数组,其中N很大,比如十亿.您想要计算此数组的中值.假设您有m+1机器(m工人,一个主人)来分配作业.你会怎么做呢?
由于中位数是一个非线性算子,你不能只找到每台机器的中位数,然后取这些值的中位数.
在我的代码中,主循环如下所示
while ( (data = foo()) != NULL ) {
// do processing on data here
}
Run Code Online (Sandbox Code Playgroud)
其中foo()用C语言编写(它使用libavcodec获取视频流中的下一帧,如果你很好奇的话).
我的问题是,由于原因太复杂而无法进入,有时会foo()挂起,这会停止整个程序.我想要做的是检测这种情况,即foo()花费超过N秒,如果是这样,采取行动.
我想创建一个单独的线程来运行foo()来实现这一点我之前没有做过任何多线程编程.这就是我想要做的事情:
foo()foo()完成后,子线程返回foo()foo()不返回null,重复步骤1-4 ,这表示结束.我该怎么做呢?我需要三个线程(主线程,运行foo()和时序)吗?
谢谢!
这让我感到沮丧.我只是想创建一个共享内存缓冲类,它在通过Boost.Interprocess创建的共享内存中使用,我可以在其中读取/存储数据.我写了以下内容来测试功能
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace std;
using namespace boost::interprocess;
int main( int argc, char* argv[] ) {
shared_memory_object::remove( "MyName" );
// Create a shared memory object
shared_memory_object shm ( create_only, "MyName", read_write );
// Set size for the shared memory region
shm.truncate(1000);
// Map the whole shared memory in this process
mapped_region region(shm, read_write);
// Get pointer to the beginning of the mapped shared memory region
int* start_ptr;
start_ptr = static_cast<int*>(region.get_address());
// Write data into the …Run Code Online (Sandbox Code Playgroud) 在准备采访时,我决定使用迭代器逻辑编写经典的"查找数组中有两个元素总结到给定数字"的问题,以便它可以推广到其他容器而不是vector.
到目前为止,这是我的功能
// Search given container for two elements with given sum.
// If two such elements exist, return true and the iterators
// pointing to the elements.
bool hasElementSum( int sum, const vector<int>& v, vector<int>::iterator& el1, vector<int>::iterator& el2 )
{
bool ret = false;
el1 = v.begin();
el2 = v.end()-1;
while ( el1 != el2 ) {
if ( *el1 + *el2 == sum ) return true;
++el1;--el2;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用,但我不知道如何在不使用条件的情况下做到这一点while ( el1 >= …