C++ :: Using Smart Pointers To Sort And Relink Potentially Large Data Elements
Feb 20, 2014
I am trying to use smart pointers to sort and re-link potentially large data elements. I have defined a class in my code for smart pointers, as listed below:
The object I'm trying to sort is a singly-linked list containing class objects with personal records (i.e., names, phone numbers, etc.). The singly-linked list class has an iterator class within it, as listed below.
The following is my function within my list class for "sorting" using the smart pointers.
template <typename T>
void slist<T>::sort(){
vector< sptr<node> > Ap(N); // set up smart point array for list
//slist<T>::iterator iter = begin();
node *ptrtemp = head->next;
[Code] .....
I must have a bad smart pointer assignment somewhere because this code compiles, but when I run it, std::bad_alloc happens along with a core dump. Where am I leaking memory?
I am new to smart pointers and have a question.If you push a smart pointer onto a vector and then some where later pop it back off it will delete the memory right?
I'm writing quite a large program that needs to work with very large arrays (up to 100 million integers). It is necessary that i can access every point of the array directly (without running through the array) and that I use as little memory as possible. I first wrote the program with pointers that point to allocated heap memory. It works fine but now I wanted to use smart pointers instead (so I'm sure to have no memory leaks). Here's a simple visualization of my problem:
#include <iostream> #include <memory> using namespace std; unique_ptr<int[]> upArray; int main() { int nArrayLength = 10;
[Code] ....
There are 2 things that do not work how I would like the to:
1. It wants me to assign the heap memory in one step: unique_ptr<int[]> upArray(new int[nArrayLength]); But I'd like to have the unique_ptr in my Class_Declaration before I know the array length and allocate the memory later. 2. *(upArray + i) = i; cout << *(upArray + i);
Suppose I'm writing a program designed to simulate a large company. I'm interested in tracking each company employee by the location where they work. This company has perhaps a thousand different locations:
class Employee { public: AccessorFunction1(); // does something AccessorFunction2(); // does something different AccessorFunction3(); // does something completely different protected: // Some data
[code]....
Once employees are created and pointers to them are saved in the proper Location vector, I write an accessor function, OrganizeLocation(), designed to do a number of operations on a given vector. The problem is, I have maybe a thousand vectors. How do I call this function and specify which vector I want?
Currently, I'm using this clunky solution:
void Company::OrganizeLocation(int a){ switch(a) { case 1: { for(unsigned int i=0; i<LocationA.size(); i++) { LocationA[i]->AccessorFunction1(); LocationA[i]->AccessorFunction2(); LocationA[i]->AccessorFunction3();
[code]....
The key point here is that whichever vector I choose to operate upon, I'll do the exact same procedure every time. I hate this solution because it results in very long and repetitive code not to mention its very error-prone when you re-editing all those "LocationA 's into "LocationB/C/D/etc."
Or, if it can't be done in C++, is there a better design approach? Ultimately I need the Company object to hold multiple vectors but use one compact accessor function to perform operations on just one of them.
So I try to sort an array with qsort and then save the new order, so this means if I have a list like:
4 3 7 2 [0] [1] [2] [3],
after sorting it becomes:
2 3 4 7 [3] [1] [0] [2] <== this is what I want to have!
Code:
void qsort_my(int *a, int l, int r, int *array_order) { int j; if( l < r ) { j = split( a, l, r, array_order); qsort_my( a, l, j-1, array_order); qsort_my( a, j+1, r, array_order);
[Code]...
But my problem is that the list gets sorted, but the indexes do strange stuff. When I run with this list:
4 8 14 1 2 1 22 12 2 14 Pos: 0 1 2 3 4 5 6 7 8 9
I get:
1 1 2 2 4 8 12 14 14 22 Pos: 1 0 1 0 0 5 7 6 5 6
And with some printfs I noticed, the first two calls of split it is fine but then strange things start to happen, what I do wrong?
so I'm trying to rework some code that solves sets of equations by gaussian elimination and wanted to change the array elements to pointers. Below i've put my c code and the custom header file that goes with it.
Header file
#ifndef CHAPTER5_8_H #define CHAPTER5_8_H #define N 5 #define INPUT_FILENAME "equations.txt"
I'm writing a program that will implement BubbleSort and MergeSort and time them as they sort a dynamic array with N elements. These are the instructions on what my main.cpp file should do.
main.cpp Include all needed libraries for timing and random number generation while the number of element, N, is less than 100001 repeat the following.
create an array with N (initially 10) random elements sort the same array with Bubble and Merge sort time how long it takes each algorithm in microseconds (see example below) Increase N by a factor of 10 (N *= 10) Hint: you may want to put your merge sort object on the heap and delete it with every iteration of this loop
So I'm attempting to write a program that will parse through a large file (genome sequences) and I'm basically wondering what options I should consider if I wanted to either:
a) store the entire genome in memory and then parse through it b) parse through a file in small portions
If I go with "a", should I just read a file into a vector and then parse through it? And if I go with "b" would I just use an input/output stream?
Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant.The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?My code is like this:
//.h file: class Time { unsigned int hours; unsigned int minutes; unsigned int seconds;
Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant. The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?
Im getting some errors in my code. I put the error as comments in the code. Each error is after the line where it occurs. My code is like this:
I have to store large amount of data and retrieve the same data then write into file in C++. Currently I am using vector to store and retrieve. But vector is taking more time to store and retrieve the element. Is any other best data structure to store and retrieve large amount of data in unordered way?
I have to write a function called sortMe that sorts the elements of an array in numerical order from highest to lowest values (descending order) or vice versa (ascending order).
The assignment asks to: NOT re-arrange elements in the array; instead, it uses a second array, an array of indexes for the elements in the original array and then sortMe sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes.
Header of the function sortMe must be as shown below:
void sortMe(int array[],int sortedIndexes [], int size, char mode)
When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order.
Declare and initialize the array array.
Declare the array sortedIndexes but do not initialize it. You are going to play with the array sortedIndexes in the function sortMe.
EXAMPLE:
int array[5]={3, 5,-1,10,0}; int sortedIndexes[5]; sortMe(array,sortedIndexes, 5, 'a');
After the function call, the elements of the array sortedIndexes should be: 2,4,0,1,3.
notice that the function does not e-arrange the elements in the array.
I am writing a contact book program in C that will run in the terminal. I am going to create a file to store the data separately, but have run into a few question areas:
1) It has occurred to me that it might be more efficient to setup the program so that it sorts the contact info by info type rather than by running through an index of the contacts and then linking all of that contact's information to that index. So basically one part of the file would be phone numbers, the other contact names, and so forth with each piece of data linked to another piece within the data file that corresponds to the same contact. Am I wrong to think this is a more efficient way to set up the file?
2)How would you link the data information to other pieces of data corresponding to the same contact in C? My thought is to do it with pointers, but I didn't know if it were possible to have the pointers as I didn't know if the pointer would point "persistently" to the data once the file closed.As I felt this is a more "in general" question, I have chosen to omit my code from this post (especially since there is not much "meat" in it to speak of).
fprintf(stderr, "Data is %s when first entering ins_llist loop. ", data); q = malloc(sizeof(struct lnode)); if ( q == NULL ) return(NULL); }
[code]...
Trying to figure out a way to sort as i enter my data into the llist. What is in red and blue is what I've been messing around with to try and see if maybe i can get it to sort but it is not. When i flip sign, the order changes opposite, but i cant see how to sort each item as i go. Maybe i am too tired right now, lol, been working on this program for what feels like 30 of the past 24 hours haha.
I want to do simple program to capture some 5 surnames of people(employees) - using array and pointers. Well I understand a bit of arrays and I know how to loop through.
How can achieve this? This holds the key to my understanding how pointers work and can be used with char or strings.
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.
The Problem You are part of a company writing a spreadsheet program. As you know, spreadsheets can be sorted on any column. You're part of the project is to write one binary tree function to sort the data [Hint: use different fields when inserting nodes in the tree.] and a second function to list it in either an ascending or descending sequence. [Note: Each of these functions may actually need to be a set of related functions.]
For sample data you will have a disk file containing information about Shakespeare's plays. Your first function should create a tree based on the sort selected by the user and the second function to display the data in the sequence selected by the user. Regardless of the column being sorted, data in individual records always be displayed in the same line of the output.
Input : Each record will contain the following information: First Performed 9 characters Printed 5 characters Title 26 characters Type 7 characters
Output : Tabular output should be aligned in columns with two spaces between each. All columns should have headings. It should be sorted on the column specified by the user.
Example (This sample data provided so you can test your program.) If the data is:
1595-96 1600 A Midsummer Night's Dream Comedy 1594-95 1623 Two Gentlemen of Verona Comedy 1596-97 1623 King John History 1597-98 1598 Henry IV, Part 1 History 1611-12 1623 The Tempest Comedy 1602-03 1623 All's Well That Ends Well Comedy
[Code]...
Source: [URL]...
Possible outputs are
1 - for a sort by title: First Performed Printed Title Type --------- ------- -------------------------- ------- 1595-96 1600 A Midsummer Night's Dream Comedy 1602-03 1623 All's Well That Ends Well Comedy 1606-07 1623 Antony and Cleopatra Tragedy 1599-1600 1623 As You Like It Comedy
[Code]....
2 - for a sort by first performed: First Performed Printed Title Type --------- ------- -------------------------- ------- 1590-91 1594? Henry VI, Part 2 History 1590-91 1594? Henry VI, Part 3 History 1591-92 1623 Henry VI, Part 1 History 1592-93 1623 Comedy of Errors Comedy 1592-93 1597 Richard III History
I am suppose to make a program that reads in data from a text file (integers only) and sorts them as it inserts them into an array of size 10. I did this using an insertion sort, which worked great. But now I am being told that I need the function has to read ALL of the numbers in the text file, not just the first 10, and I am not allowed to store them THEN sort, it has to be sorted as being stored.