|
All right.
But there is another question, about this code:
vector<int> va, vb, v;
set_union::(va.begin(), va.end(), vb.begin(), vb.end(), v.begin());</int>
It courses a runtime error, because I haven't make v large enough.
I don't know why it is designed this way, why it can't be done inside the function.
|
|
|
|
|
That would pose a serious limitation to how the function can be used.
For example, this would not be possible.
set_union(v1.begin(), v1.begin() + 3, v2.begin(), v2.end(), v1.begin() + 4);
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
followait wrote: I don't know why it is designed this way, why it can't be done inside the function.
In effect, it can be - use a back_inserter (which uses push_back to insert items) rather than a plain output iterator. To minimise allocations, use the reserve member of vector .
vector va, vb, v;
v.reserve(va.size() + vb.size());
set_union::(va.begin(), va.end(), vb.begin(), vb.end(), std::back_inserter(v));
That's one of those tricksy iterator adaptors I mentioned in an answer to one of your previous queries...
[edit]PS - I've told you why it's designed this way - to maximise generality.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
The current application i'm working on has a single vector having a list of structures with data as well as a flag set (true/false). If flag is true , i'll add the vector data to string A else string B.
Is there a performance difference of having 2 vectors rather than having a single vector or it doesn't matter?
Thanks in advance,
Mayur M
|
|
|
|
|
mayur8u wrote: Is there a performance difference of having 2 vectors rather than having a single vector or it doesn't matter?
I don't think there'll be any significant performance difference.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I have the following codes (partially copied for illustration) using template class:
template<typename T>
struct _BoundingBox
{
T x0, x1, y0, y1;
};
template<typename T>
class CMyObject
{
public:
typedef struct _BoundingBox<t> stBBOX;
CMyObject()
{
Init(); // do class initialization here
}
CMyObject(const CMyObject<t> &Obj) // copy constructor
{
// do some data copy here
....
_Box = Obj.GetBountingBox();
}
virtual ~CMyObject() {}
public:
stBBOX GetBoundingBox() { return _Box; }
protected:
stBBOX _Box;
};
int main(argc, char* argv[])
{
CMyObject<int> obj1
CMyObject<int> obj2(obj1);
}
After compile (using VC 2005, the compiler gave the following error message:
error C2662: 'CMyObject<t>::GetBoundingBox' : cannot convert 'this' pointer from 'const CMyObject<t>' to 'CMyObject<t> &'
with
[
T=int
]
Conversion loses qualifiers
However, if I change
_Box = Obj.GetBountingBox();
to
_Box = Obj._Box;
in the copy constructor, the error disappears.
Anyone knows what the problem is? Thanks. </t></t></t>
<div class="ForumMod">modified on Tuesday, February 24, 2009 5:18 PM</div>
|
|
|
|
|
You need to make GetBoundingBox a const method of CMyObject :
stBBOX GetBoundingBox() const { return _Box; }
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks. I tried according to your suggestion and it worked.
But i still didn't get it why GetBoundingBox() should be a const function? what if the return value is a pointer?
|
|
|
|
|
The return value doesn't matter - it's where you use GetBoundingBox:
CMyObject(const CMyObject<t> &Obj)
{
....
_Box = Obj.GetBountingBox();
}
You are saying that Obj is const. Given that, you can only perform const methods on Obj. So, to use GetBoundingBox there, it has to be a const method.
A const method is making a promise that it doesn't alter the object on which it's called.
_Box = Obj._Box is implicitly 'const', as you are reading a member variable (it's an rvalue).
The error message gcc gives for your code is a bit better than VC++'s:
a.cpp: In copy constructor ‘CMyObject<T>::CMyObject(const CMyObject<T>&) [with T = int]’:
a.cpp:34: instantiated from here
a.cpp:20: error: passing ‘const CMyObject<int>’ as ‘this’ argument of ‘_BoundingBox<T> CMyObject<T>::GetBoundingBox() [with T = int]’ discards qualifiers
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I get an access violation when trying to add to a map in my release build. Since it works fine in debug, and if I turn off optimisations in release, I know its something I'm doing wrong. But I don't know what...
Interestingly (?), the code that fails is in a loop which works on the first iteration, and fails on the second, suggesting perhaps a problem with initialising (or reinitialising) something.
Any help or tips very much appreciated
The code that fails is (bold line):
ManInfo mi;
...
...
if (m_ManMap.count(lSubmainID) == 0)
{
mi.SubmainJunctionPt = m_pCADEngine->GetEntityPoint(pSubmainJnc->GetHandle(), 1);
mi.LatEndPt[0] = m_pCADEngine->GetEntityPoint(pFreeEnd->GetHandle(), 1);
mi.LatLen[0] = mi.SubmainJunctionPt.GetDistanceTo2D(mi.LatEndPt[0]);
mi.DistToRef = mi.SubmainJunctionPt.GetDistanceTo2D(m_ManRefPt);
m_ManMap[lSubmainID] = mi;
}
else
{
...
I have a structure thus:
struct ManInfo
{
Point3D SubmainJunctionPt;
Point3D LatEndPt[2];
double LatLen[2];
double DistToRef;
ManInfo()
{
SubmainJunctionPt.Construct(NO_ELEVATION, NO_ELEVATION, NO_ELEVATION);
LatEndPt[0].Construct(NO_ELEVATION, NO_ELEVATION, NO_ELEVATION);
LatEndPt[1].Construct(NO_ELEVATION, NO_ELEVATION, NO_ELEVATION);
LatLen[0] = -1.0L;
LatLen[1] = -1.0L;
DistToRef = -1.0L;
}
ManInfo& ManInfo::operator= (const ManInfo& second)
{
if (this != &second)
{
SubmainJunctionPt.Construct(second.SubmainJunctionPt);
LatEndPt[0].Construct(second.LatEndPt[0]);
LatEndPt[1].Construct(second.LatEndPt[1]);
LatLen[0] = second.LatLen[0];
LatLen[1] = second.LatLen[1];
DistToRef = second.DistToRef;
}
return *this;
}
};
And a map member variable thus:
typedef map<LONG, ManInfo> ManMap;
ManMap m_ManMap;
|
|
|
|
|
I don't know if it helps determine what the problem is, but I can fix it by disabling inline function expansion...
|
|
|
|
|
Given you've got an assignment operator, do you need a copy constructor as well? And a destructor?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Good suggestion Stuart, given that:
In C++, the implicitly declared assignment operator is a public and inline member function.
but it didn't help I'm afraid...
|
|
|
|
|
Well, reading around I see there are possible problems with static storage when using STL across DLLs and EXEs. My struct and map are defined and used in a DLL, but the class is dllexport ed to the EXE.
I've found that to make this work I have to turn off inlining in both the DLL and the EXE...so perhaps this is the issue??
|
|
|
|
|
That (very probably) explains it all if your C runtime is statically linked in the EXE or the DLL or both. If so, use a DLL runtime in DLL and EXE.
The reason this scenario ACCVIOs is that the DLL and EXE are using different heaps, whereas if they're using a DLL runtime, they use the same one.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks for looking at this Stuart, but still no joy. Both my EXE and DLL link the MT DLL runtime - unless its the multithreaded version that causes the problem... either way, since I need to make progress here, I switched out the std::map for a CMap and I'm golden
|
|
|
|
|
I don't have time to figure out why this doesn't work, so I just changed it to CMap instead!
|
|
|
|
|
Damn - I hate not being able to crush a bug
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I'm having the same problem. Are you inheriting from CObject and passing an instance to your View(s) via UpdateAllViews( , , instance )?
|
|
|
|
|
I have derived from the interface(IPimSrcContactNew),but the system did not invoke this API(IPimSrcContactNew::Create)?
Please help me. Thanks.
|
|
|
|
|
You need to do more than just inherit from it - it has to be a proper COM object.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Could you tell me in detail. thanks.
|
|
|
|
|
Not really - there's more to COM than I can tell you - you're going to have to learn about the technology underpinning your task.
Look for some COM/ATL tutorials like this one[^].
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
list<int> l1,l2;
...
find(l1.begin(), l2.end(), 1)</int>
This code compiles, so I think it is better to use following symantic:
find(l1, IndexBegin, count, 1);
It is more consistent, isn't it?
|
|
|
|
|
Iterators are a great way to traverse a list.
This makes the find algorithm to be used with any sort of container that implements iterators.
This way STL is able to have one single find algorithm that works with several different containers.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|