c++98

Insert multiple values into vector

余生长醉 提交于 2019-12-14 03:46:14
问题 I have a std::vector<T> variable. I also have two variables of type T, the first of which represents the value in the vector after which I am to insert, while the second represents the value to insert. So lets say I have this container: 1,2,1,1,2,2 And the two values are 2 and 3 with respect to their definitions above. Then I wish to write a function which will update the container to instead contain: 1,2,3,1,1,2,3,2,3 I am using c++98 and boost. What std or boost functions might I use to

Forward declarations and shared_ptr

妖精的绣舞 提交于 2019-12-13 11:39:42
问题 I'm trying to refactor my code so that I use forward declarations instead of including lots of headers. I'm new to this and have a question regarding boost::shared_ptr. Say I have the following interface: #ifndef I_STARTER_H_ #define I_STARTER_H_ #include <boost/shared_ptr.hpp> class IStarter { public: virtual ~IStarter() {}; virtual operator()() = 0; }; typedef boost::shared_ptr<IStarter> IStarterPtr; #endif I then have a function in another class which takes an IStarterPtr object as

May I use a constant number to choose a class at compile time, possibly using templates?

喜欢而已 提交于 2019-12-12 07:37:43
问题 Let's say I have a constant value (possibly of some enum type). Let's say I have many classes A, B, D, etc. Can I have something like this? C<1> anInstanceOfA; //This will be of type A C<2> anInstanceOfB; //This will be of type B C<3> anInstanceOfD; //This will be of type D So, is it possible to select a class based on a constant number at compile time? The general problem is that I am trying to select a functor based on a table, in which the index is an enum. I would like to avoid

How to fill vector with images in OpenCV? C++

大兔子大兔子 提交于 2019-12-12 05:07:02
问题 I created vector std::vector<cv::Mat> main_layers; placed in a class. The vector has not been initiated yet. I have also public member cv::Mat source; initiated in constructor initiation list. Now I have a copy method to copy segments of the image to main_layers: void copy(){ Rect roi; auto primarySegment = main_layers.begin(); for (int c = 0; c< primaryKernelsLoad; c++) { if (heightPriority) { roi = Rect(0, c, size.width, segment1Size); source(roi).copyTo(primarySegment); auto nx = std::next

When initializing a struct with ={} syntax, what's happening under the hood?

我的未来我决定 提交于 2019-12-12 03:55:41
问题 edit Tweaked example a little based on comments A little code then the question (just to clarify, this is a C++ question): #include <cstdio> struct MYSTRUCT1 { int asdf[4]; } MyStruct1; struct MYSTRUCT2 { int asdf[4]; MYSTRUCT2() : asdf() {} } MyStruct2; template <class T> void test() { T blah = {{1,-1,1,-1}}; for( int ii = 0; ii < 4; ii++ ) { printf( "%d ", blah.asdf[ii] ); } printf( "\n" ); } int main() { // Works fine; MyStruct1 doesn't define a constructor test<MyStruct1>(); // Doesn't

How to deal with multiple parameters of different types in C++98?

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:39:19
问题 In order to implement a thread class(In C++98 and Windows.h ). I have something like this: Thread::Thread(_beginthreadex_proc_type fn) { m_raw = fn; m_args = 0; m_handle = 0; m_id = 0; } The code above works fine it takes a function that not receive parameters, and with the next code it function is called by a new thread: void Thread::Join() { m_handle = (HANDLE)_beginthreadex(0, 0, m_raw, (m_args ? m_args : 0), 0, 0); if (m_handle) WaitForSingleObject(m_handle, INFINITE); } This code also

can't pass by reference a pointer returned from a function

99封情书 提交于 2019-12-11 08:17:07
问题 why I need intermediate variable to pass my return pointer by reference instead of just using the function that returns that pointer ? This doesn't compile int main () { testfunc(getpointer()); return 0; } error: C2664: 'void testfunc(int *&)': cannot convert argument 1 from 'int *' to 'int *&' and this compiles int main () { int *i = getpointer(); testfunc(i); return 0; } my two functions void testfunc(int *& i) // I have to use this interface { cout << i[0] <<endl; } int* getpointer() { int

How to initialize const size array in constructor without explicitly stating the size in class declaration?

邮差的信 提交于 2019-12-11 05:39:22
问题 I need to do something like class foo { foo(); int a[]; } Further in cpp-file: foo::foo() : a{1,2,3,4} {} Peculiarities: C++98 standard array is not int, initializing list is much longer. Though its values are known at compile time, they can change from time to time EDIT: Another option is also suitable, when new[] is used for memory allocation, but, again, it is undesirable to state the array size explicitly: class foo { foo(); int * a; } Further in cpp-file: foo::foo() { a = new[]; //

Conversion from null-integer to pointer in comma list

家住魔仙堡 提交于 2019-12-11 04:01:47
问题 I know that in our modern world NULL and 0 are not best practices in operating with pointers, and according to cppreference: Pointer conversions A null pointer constant (see NULL), can be converted to any pointer type, and the result is the null pointer value of that type. Such conversion (known as null pointer conversion) is allowed to convert to a cv-qualified type as a single conversion, that is, it's not considered a combination of numeric and qualifying conversions. But why this code is

Implementing a spinlock in Boost. Example Needed

自闭症网瘾萝莉.ら 提交于 2019-12-11 03:27:17
问题 I wanted to know if boost has any libraries that assist in implementing spin locks. I know boost supports mutexes but I could not find any examples that show or describe spinlocks in boost.Any examples showing how to implement a spin lock using boost(preferably) would be appreciated.(C++98) 回答1: Example using Boost.Atomic: #include <boost/atomic.hpp> class SpinLock { boost::atomic_flag flag; // it differs from std::atomic_flag a bit - // does not require ATOMIC_FLAG_INIT public: void lock() {