Storing pointer as a key in std::map


To the question is storing pointers as key in std::map safe I got a negative reply in a forum I use. But I am still tempted to use it. What are your suggestions Am I really looking for trouble -- ...
Posted On: Monday 26th of November 2012 12:17:07 AM Total Views:  61
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




Reading a file and storing it as a 2d Array.

Hi , I have been given the assignment to create a program to find the the least squares method of fitting a straight line function to one of two data sets given to me in a txt format. the file has an unknown number of data points. The format of the data file is x, y, error x, error y. The data should be read in from the file and then the various sums calculated so that the a and b coefficients and their associated error can be found I have to read the data into an array before performing the sums as it will demonstrate that i know how to use multidimensional arrays(well that's the idea). Finally, i have to make the program modular so that there is a separate function for reading in the data and a function for calculating the least squares coefficients. As yet i have searched the internet and my notes and i can open the the file and with fgets and fputs print to screen but i think as this works with strings i cant see how i can then get the information and put it into a 2d array from there. Unfortunately my file will not print at all now that i have tried to put it into a 2d array. the source file looks a bit like: 13.079 0.051658 0.77359 0.00052980 14.162 0.051750 0.0097982 0.0019695 15.888 0.054027 0.80159 0.0019485 16.191 0.054512 0.62303 8.9929e-05 17.339 0.055797 0.99587 0.00041386 18.784 0.056146 0.25421 0.0016562 19.967 0.056846 0.60045 0.00038078 20.437 0.058294 0.61663 0.00014638 21.397 0.058011 0.026190 0.00032348 22.998 0.059799 0.26090 0.00092712 23.456 0.059883 0.64564 0.0016402 24.070 0.060772 0.83006 0.0017486 25.109 0.060415 0.80091 0.00093706 26.887 0.061940 0.80918 6.3588e-05 27.527 0.063505 0.86446 0.0012275 28.860 0.064270 0.31924 0.0014887 29.616 0.063735 0.63400 0.0016803 30.430 0.065634 0.64624 0.00025035 31.863 0.067435 0.20427 0.00051878 32.326 0.067050 0.0066200 0.00017732 33.043 0.068377 0.71793 0.0017101 and my code at the moment is: Code: #include int main(int argc,char ** argv) { int i, j; int nrows; int ncolumns; char filename[20]; FILE * userfile = 0; printf("Please Enter The Name Of The File You Would Like To Fetch\n"); scanf("%s", filename); userfile = fopen(filename, "r"); char buffer[nrows][ncolumns]; if (userfile) { printf("file opened\n"); while(fgets(buffer, 200, userfile)) { printf("%s", buffer);} fputs(buffer, userfile); for (i=0;i
VIEWS ON THIS POST

104

Posted on:

Saturday 22nd September 2012
View Replies!

storing lines of file

I am trying to read in a source file and store each line in a linked list using structs. But for some reason it keeps on segfaulting when i try to compile and run the program. I checked gdb and its complaining about the line: printf("%s",temp->line); And i checked the local variables. For some reason when it gets to the outer while loop with the fgets(), i can't see any of the temp variables. Does anyone know what might be the problem I can't figure out why the temp variables are non-accessible inside the main while loop. Stepping through the code, after the temp->line = *buffer; when i go to check the temp->line in gdb, it says address out of bounds. I am not sure why. Thank you for any help. Code: #include #include #include typedef struct node{ struct node *next; char *line; } node_t; ... void modify(){ int i=0; char* buff; char buffer[128]; FILE *fp=fopen("thisfile.c","rw"); node_t *str_node = NULL; node_t *head = NULL; //while(EOF != fscanf(fp, "%c",buff[i++]){} while(fgets(buffer, sizeof(buffer), fp)!=NULL) { node_t *temp1; temp1 = (node_t*) malloc(sizeof(node_t)); //temp1->line = *buffer; temp1->next = head; head = temp1; while(temp1->next != NULL) { temp1 = temp1->next; } //str_node.line = *buffer; node_t *temp; temp=(node_t*) malloc(sizeof(node_t)); temp->line = *buffer; temp->next = NULL; temp1-> next = temp; printf("%s",temp->line); } fclose(fp);
VIEWS ON THIS POST

218

Posted on:

Tuesday 9th October 2012
View Replies!

storing lines of file

I am trying to read in a source file and store each line in a linked list using structs. But for some reason it keeps on segfaulting when i try to compile and run the program. I checked gdb and its complaining about the line: printf("%s",temp->line); And i checked the local variables. For some reason when it gets to the outer while loop with the fgets(), i can't see any of the temp variables. Does anyone know what might be the problem I can't figure out why the temp variables are non-accessible inside the main while loop. Stepping through the code, after the temp->line = *buffer; when i go to check the temp->line in gdb, it says address out of bounds. I am not sure why. Thank you for any help. Code: #include #include #include typedef struct node{ struct node *next; char *line; } node_t; ... void modify(){ int i=0; char* buff; char buffer[128]; FILE *fp=fopen("thisfile.c","rw"); node_t *str_node = NULL; node_t *head = NULL; //while(EOF != fscanf(fp, "%c",buff[i++]){} while(fgets(buffer, sizeof(buffer), fp)!=NULL) { node_t *temp1; temp1 = (node_t*) malloc(sizeof(node_t)); //temp1->line = *buffer; temp1->next = head; head = temp1; while(temp1->next != NULL) { temp1 = temp1->next; } //str_node.line = *buffer; node_t *temp; temp=(node_t*) malloc(sizeof(node_t)); temp->line = *buffer; temp->next = NULL; temp1-> next = temp; printf("%s",temp->line); } fclose(fp);
VIEWS ON THIS POST

256

Posted on:

Tuesday 16th October 2012
View Replies!

Issue storing Prime numbers in an array and then prinitng

Hi I am trying to write a program that generates the prime numbers under a 1000. The program itself works fine until i edit it and try to store the values in an array and then print that array. I know im pretty close to fixing this but i cant seem to find where im going wrong. Any help would be great Code: #include int main () { int array[1000]; int j=0; int i = 0; /*assigns value to i*/ printf("The following numbers are prime:\n\n"); while (i < 1000){ int num = 2; int non = 0; while ( num < i ){ if ( i%num == 0) { /*true statement means number is not prime */ /*adds 1 to value of i in each time*/ non++; } num++; } if (non ==0){ scanf("%d",&array[i]);/*prints the prime number for the user */ } i++;/*adds 1 to value of count*/ } i=0; while (i
VIEWS ON THIS POST

241

Posted on:

Wednesday 17th October 2012
View Replies!

storing integers in an array

i ma currently writing a program that converts numbers to words. but this part stores the characters in an array of nine elements. this code is giving me heat..... help me out with this Code: //read_int: This function reads the integers from input checks and stores them in an array. int read_int(int num[], int n) { int ch; i = 0; printf("\nEnter number Here: "); while( (ch = getchar())!= '\n') { if ( ch == 'Q' || ch == 'q') { printf("\nExiting...."); printf("\
VIEWS ON THIS POST

146

Posted on:

Saturday 20th October 2012
View Replies!

storing static data of variable types

I've got to write a BASIC to C++ converter and came across the problem of the DATA statement. I must provide a way of storing: DATA 5.123, "abc", ... in C++. my first guess was this: struct { double* pD; const char* pC; } str[2]={ {NULL, "xy"}, {&123.4, NULL}, }; but &123.4 is not possible. Now, having temporary global variables for this doesn't make me too happy either. Is there a "clean" way of doing this -- ------------------------------------ Gernot Frisch http://www.glbasic.com
VIEWS ON THIS POST

151

Posted on:

Sunday 4th November 2012
View Replies!

Issues related to storing heterogeneous-sized objects in a vector

Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. : Please consider the snippet below. I have some objects which are derived (subclassed, subtyped) from simpler ones, in increasing size. There is a linear hierarchy. I need to keep them all in some sort of linked list (perhaps std::vector). I could have several vectors, one for every different class, but I have an intuitive belief that C++ must be able to provide a means to save all the objects in the same vector. Issue #1: Notice the definition of the vector. Should I use the largest class (vector) to reserve space for the extreme case Or, should I define a vector instead since the others are inheritors of the triangle class Maybe my approach is wrong to begin with TIA, -RFH ------------------- #include using namespace std; struct point { int x, y; }; class triangle { public : point vertex1; point vertex2; point vertex3; }; class square : public triangle { public : point vertex4; }; class pentagon : public square { public : point vertex5; }; int main() { vector piggybank; triangle *love_affair = new triangle; square *boxing_ring = new square; pentagon *military = new pentagon; military->vertex5.x = 123; military->vertex5.y = 456; piggybank.push_back(*military); piggybank.push_back(*boxing_ring); piggybank.push_back(*love_affair); return 0; }
VIEWS ON THIS POST

162

Posted on:

Sunday 4th November 2012
View Replies!

pasing an array pointer and storing as a class member

(apologies for cross posting from the moderated group..i'm sure you understand) , I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member for future use. So far, I'm just causing crashes, psuedo code below: double block[8192]; foo = MyClass(block); //// MyClass { public: MyClass (double *blk) {// constructor inBlk = blk; } Process { // this method will be called periodically and // iterate over array pointed to by inBlk // ..which causes a crash } private: double *inBlk; } I imagine I'm doing something stupid that I just can't see at the moment. How might one solve this Perhaps ideally I should create the array within MyClass but I don't really want to right now.
VIEWS ON THIS POST

139

Posted on:

Monday 5th November 2012
View Replies!

problems of storing dynamically created objects in a vector

Ian Collins wrote in message... > ref: > > Finally, can someone please tell me what's the correct way to store > > dynamically created objects into a vector > > > It depends on the size of the object, for small objects you may as well > sore by value, for larger ones, store a pointer and clean up when you > erase elements from the vector. For this, I would suggest you use some > form of smart pointer to do the hose keeping for you. Hi Ian, Sometimes 'typos' are bad, sometimes they are good. By the time I got through 'sore by value' and 'hose keeping', I was in belly-laughs.
VIEWS ON THIS POST

239

Posted on:

Monday 5th November 2012
View Replies!

Problem in restoring exchange server database.

Hi all I am trying to take exchange server 2003 backup and restore programmatically. My backup function works fine. But i am in problem in restoring database. My HrESERestoreAddDatabase is not working well. The HrESERestoreAddDatabase is returning HRESULT -939587631. Will anyone give me any link related exchange server 2003 backup Or can anyone give me sample code or link to sample code related to HrESERestoreAddDatabase() method
VIEWS ON THIS POST

95

Posted on:

Wednesday 7th November 2012
View Replies!

beginner question about storing binary data in buffers, seeing binarydata in a variable, etc

Im working on an assignment that has me store data in a buffer to be sent over the network. I'm ignorant about how C++ stores data in an array, and types in general. If i declare an array of chars that is say 10 bytes long: char buff[10]; does this mean that i can safely store 80 bits of data When i think of an array of chars, i think each spot in the array as a sequence of 8 1's or 0's. Is this a correct visualization I guess my question here is why do most buffers seem to be implemented as char arrays Can any binary value between 0 and 255 be safely put into a char array slot (00000000 to 11111111). Why not implement a buffer using uint8_t Obviously I have a very loose grasp on how buffers are saving data, and how a receiver gets this data on their end. I understand the sockets stuff, just not the buffer-specific stuff. Any enlightenment would be most appreciated.
VIEWS ON THIS POST

113

Posted on:

Wednesday 7th November 2012
View Replies!

stl heap question: restoring heap validinty after changing 1 element

I have vector 'vec' of integers which was made into valid heap using std::make_heap(). Then we change value of a single element vec[k], like: vec[k] = new_value; Now this makes vec not a valid heap. Heaps have simple algorithm that restores "heap validity" in O(log N)after one element was changed, knowing index of changed element. But I cannot find this function in stl. Which stl function restores my heap vaidity in O(log N) after single element change, given known index k of changed element
VIEWS ON THIS POST

161

Posted on:

Wednesday 7th November 2012
View Replies!

storing a list of struct in a file

Hi all, I would like to store (and read) a list of structs into a file using c+ + (each struct contains integers and strings), and I'm wondering if it there is a method to do it at "high level", I mean: I would like to don't care of how the strucs are saved on the file, I want to just save/load my list, is it possible using the std
VIEWS ON THIS POST

115

Posted on:

Wednesday 7th November 2012
View Replies!

problem storing different iterators in a container

I'm trying to write a functor-class, which supports the de- multiplexing of data from one input-container to two or multiple output containers (de- interleaving). For example: vec1 = [1 5 2 6 3 7] #demultiplex vec1 -> result1,result2 ...
VIEWS ON THIS POST

338

Posted on:

Sunday 11th November 2012
View Replies!

Smart pointer and thread programming

In multi-thread programming, if I using smart pointers to store pointers to the same dynamically allocated objects, can memory leak be avoided For example, two threads share the same dynamically allocated objects using smart pointer. The reference to the ...
VIEWS ON THIS POST

95

Posted on:

Monday 26th November 2012
View Replies!

a pointer to a derived class problem

hello all, having a problem with derived classes. i'll show by pseudo code example. first the base class and derived classes: class Shape { // base class for all shapes public: virtual void Draw() {// draw code here} }; class ...
VIEWS ON THIS POST

82

Posted on:

Monday 26th November 2012
View Replies!

About the retrogression problem when passing a array variable into a function which takes a pointer as its argument.

folks, As the Subject suggests, array variable will retrogress as the parameter of the function actually taking pointer as its argument, like this: int f(int* i) { cout << sizeof(i) << endl; return 0; } main() { int a[100]; ...
VIEWS ON THIS POST

181

Posted on:

Monday 26th November 2012
View Replies!

c struct with pointers assignment

, I have a struct with pointers. I am trying to assign and instance of the struct but keep getting a seg fault. I am new to c, so I am uncertain of how to handle this.
VIEWS ON THIS POST

76

Posted on:

Monday 26th November 2012
View Replies!

Downcast with smart pointers?

, I'm having a design issue that I can't solve. I'm getting from a network a stream of data, which is a http message (my program is neither a client or server, it just catches messages over the network) For ...
VIEWS ON THIS POST

78

Posted on:

Monday 26th November 2012
View Replies!

data corruption on pointer cast

llo, Names matt, I implemented a set class as follows: template class Element { public: virtual int operator == (T) = 0; virtual int hash() = 0; }; /** * A efficient hash implementation of a Queue-set that does ...
VIEWS ON THIS POST

83

Posted on:

Monday 26th November 2012
View Replies!