Click here to Skip to main content
15,898,987 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: How do I read one vector from another? Pin
berndg17-Nov-03 20:31
berndg17-Nov-03 20:31 
GeneralRe: How do I read one vector from another? Pin
WREY17-Nov-03 22:07
WREY17-Nov-03 22:07 
GeneralRe: How do I read one vector from another? Pin
berndg17-Nov-03 22:25
berndg17-Nov-03 22:25 
GeneralRe: How do I read one vector from another? Pin
WREY17-Nov-03 23:28
WREY17-Nov-03 23:28 
GeneralRe: How do I read one vector from another? Pin
Andreas Saurwein21-Nov-03 11:31
Andreas Saurwein21-Nov-03 11:31 
GeneralRe: How do I read one vector from another? Pin
WREY24-Nov-03 14:48
WREY24-Nov-03 14:48 
GeneralRe: How do I read one vector from another? Pin
Andreas Saurwein24-Nov-03 15:33
Andreas Saurwein24-Nov-03 15:33 
QuestionWill sort method in STL cause memory disorder? Pin
Yu Yang15-Nov-03 9:40
Yu Yang15-Nov-03 9:40 
Hi, everyone

I meet some problem when I try to use the sort algo in STL to sort a structure that contains pointers. I am sure I have define the proper copy constructor to avoid memory disorder. However, it seems that the sort function disorder the memory. Could you give some advice on it? The following is the test code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <sys types.h="">
using namespace std;

struct str_info
{
string content;
size_t occ_sum;
long * occ_pos; // occurrence

public:
str_info(): content(""), occ_sum(0),occ_pos(NULL){}
~str_info() { if (occ_pos != NULL) delete[] occ_pos; occ_pos = NULL; }

str_info(const str_info& info){
content = info.content;
occ_sum = info.occ_sum;
occ_pos = new long[occ_sum+1];
memcpy(occ_pos, info.occ_pos, sizeof(long)*occ_sum);
}
};

class cmp_str_info
{
public :
int operator()(const str_info& info1, const str_info& info2) const
{
return strcmp(info1.content.c_str(), info2.content.c_str()) < 0;
}
};

void init(str_info * info)
{
info[0].content = "info0";
info[0].occ_sum = 1;
info[0].occ_pos = new long[info[0].occ_sum];
info[0].occ_pos[0] = 45;

info[1].content = "a_info1";
info[1].occ_sum = 1;
info[1].occ_pos = new long[info[1].occ_sum];
info[1].occ_pos[0] = 55;

info[2].content = "b_info2";
info[2].occ_sum = 1;
info[2].occ_pos = new long[info[2].occ_sum];
info[2].occ_pos[0] = 35;
}

int main()
{
vector<str_info> vstr;
vstr.clear();

str_info info[3];
init(info);

vstr.push_back(info[0]);
vstr.push_back(info[1]);
vstr.push_back(info[2]);

vector<str_info>::iterator it;

cout<<"Before sorting....\n";
for (it = vstr.begin(); it != vstr.end(); it++)
{
cout<<it->content<<" "<<it->occ_pos[0]<<endl;
}

str_info *="" infos="new" str_info[3];
infos[0]="

sort(vstr.begin()," vstr.end(),="" cmp_str_info());

cout<<"\nafter="" sorting...\n";
for="" (it="vstr.begin();" it="" !="vstr.end();" it++)
{
cout<<it-="">content<<" "<<it->occ_pos[0]<
AnswerRe: Will sort method in STL cause memory disorder? Pin
jbarton17-Nov-03 5:20
jbarton17-Nov-03 5:20 
GeneralHINSTANCE Pin
alex.barylski15-Nov-03 8:36
alex.barylski15-Nov-03 8:36 
GeneralRe: HINSTANCE Pin
Michael Dunn15-Nov-03 16:39
sitebuilderMichael Dunn15-Nov-03 16:39 
GeneralCImageList Pin
alex.barylski14-Nov-03 14:47
alex.barylski14-Nov-03 14:47 
GeneralRe: CImageList Pin
Michael Dunn14-Nov-03 16:00
sitebuilderMichael Dunn14-Nov-03 16:00 
GeneralRe: CImageList Pin
alex.barylski14-Nov-03 16:58
alex.barylski14-Nov-03 16:58 
GeneralRe: CImageList Pin
Michael Dunn14-Nov-03 18:20
sitebuilderMichael Dunn14-Nov-03 18:20 
GeneralRe: CImageList Pin
umeca7414-Nov-03 23:04
umeca7414-Nov-03 23:04 
GeneralEnter key in rich edit control Pin
ted_yu14-Nov-03 7:48
ted_yu14-Nov-03 7:48 
GeneralComposite control Drag &amp; Drop Pin
Member 31248914-Nov-03 0:19
Member 31248914-Nov-03 0:19 
Generalini file Pin
jasu12-Nov-03 22:36
jasu12-Nov-03 22:36 
GeneralWTl in ATL 7 control Pin
Gabriel.P.G12-Nov-03 9:25
Gabriel.P.G12-Nov-03 9:25 
GeneralProblem with atl vc7 and sinkmap Pin
_Magnus_12-Nov-03 0:48
_Magnus_12-Nov-03 0:48 
GeneralRe: Problem with atl vc7 and sinkmap Pin
geo_m12-Nov-03 3:04
geo_m12-Nov-03 3:04 
GeneralRe: Problem with atl vc7 and sinkmap Pin
_Magnus_12-Nov-03 3:25
_Magnus_12-Nov-03 3:25 
GeneralRe: Problem with atl vc7 and sinkmap Pin
Michael Dunn12-Nov-03 7:25
sitebuilderMichael Dunn12-Nov-03 7:25 
GeneralRe: Problem with atl vc7 and sinkmap Pin
_Magnus_12-Nov-03 21:46
_Magnus_12-Nov-03 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.