C++ :: Vector Like Container Which Can Reuse Space?
Dec 24, 2014
Unoptimized problem: I work on a table (vector of vectors). vector< vector<double> >
Optimization: If the current set of vectors is v1...vn (so each vk is a vector of double), my problem is such that I will only work on the last M of the vectors, the earlier vectors are irrelevant. I would like to reuse space to make the algorithm scalable, so I want to delete the vectors that can be forgotten. I read in a new vector, I delete the oldest vector.
I am trying to pass a 2D array called f (coming from a text file with 9 columns of numbers and 297,680 rows) that was created using the vector container in my main() to the function myfunc. I'm just trying to figure out how to pass the address of f in main() to myfunc(), so that myfunc() has arguments consisting of a pointer g (that accepts the address of f as an input) and an int.
This is the error from the compiler: test_2d.cc: In function ‘int main()’: test_2d.cc:47:25: error: cannot convert ‘std::vector<std::vector<double> >*’ to ‘double (*)[297680][9]’ for argument ‘1’ to ‘int myfunc(double (*)[297680][9], int)’ return myfunc(&f,count); ^ Here is my code:
#include <iostream> #include <fstream> #include <iomanip> //allow setprecision to get all the decimal points #include <vector> #include <string>
How can I write my own container which has properties of both vector and list.
E.g. I can access elements directly using [] operator like vector and behave like list in terms of memory (means we don't have to shift elements if we want to insert in between like list)....
I will try my best to describe this problem I am having because I am not sure if I can reproduce the error in a small code example as this is part of a large code project.
I have a plain std::vector which contains pointers to objects.
When attempting to delete the objects, even if I know for a fact that the std::vector has at least one object in it as shown by the debugger, I get the "Vector Subscript Out of Range" error.
I'll even do a range for loop like so:
for (auto & e : CurrentObjects) { delete e; }
And yet I still get the "Vector Subscript Out of Range" error. The destructor is never called for the object represented by "e" so I am not sure why I get the error.
I have a question about memory allocation.I have a function that calls a lot of object constructors, which in return these constructors will allocate a lot of memory.Now, in my program I am sure that if I first call this function , say it will call the constructor of 100 object.If I call this function again and again, I am sure that it will only call the constructor 100 times again, and thus I am sure that the memory allocated in the first call can be reused again.
How can I reuse the memory allocated in the first call?Can I use something like boost:object_pool so that I can tell the pool to restart from the begining and do not allocate extra memory, just use what you already have?
Im tasked with reading a data file, this is an example snippet
list of trophy winners year facup leaguecup 1stdiv 2ndiv 1960/61 Tottenham Hotspur Aston Villa Tottenham Hotspur Ipswich Town 1961/62 Tottenham Hotspur Norwich City Ipswich Town Liverpool 1962/63 Manchester Utd Birmingham City Everton Stoke City
The file starts in 1892 and is up to 2011/12, there is data missing for some years due to the wars etc,
once ive read the file, i need to store the data, for me to re-use.
There are a lot of useful link regarding reading data in, but they tend to be with very small files with say 10 lines of numbers.
I've got some code in C++ that does some basic analysis on price data and I run it from the cmd prompt. One of the functions I had built to read in the data is as follows:
How would one merge this type of code into a Qt GUI? Ideally, I'd like to use something like QFileDialog to open up a folder with my data files, select a file, and then read the file that the user selects. Would the main.cpp look something like this?
int main (int argc, char *argv []) { QApplication prog (argc, argv); QPushButton *load_button = new QPushbutton ("Load File"); QObject:: connect (load_button, SIGNAL (clicked()), &prog, SLOT ( (ReadPricesfromFile function?); load_button-> show (); return prog.exec ();}
is it better to reuse an existing variable or create a new variable for the following situation:
/* Is this faster: */ glm::mat4 newViewModelMatrix = (*existingViewMatrix) * (*existingModelMatrix); /* Or is this faster? */ glm::mat4 existingViewModelMatrix; /* declared in class header - initial value assigned in class constructor */ existingViewModelMatrix = (*existingViewMatrix) * (*existingModelMatrix);
So I'm writing an RPG and I'm in need of an inventory system. Of course as an relatively old member of the forum I know best than just come here and ask so I've already researched quite a bit and I've formulated this idea.
I've kind of conceptualized it like so: I'll have some sort of STL container of a unique_ptr of my base item class. There will be derived item classes. Taking advantage of polymorphism I can then call the new Derivedclass when inserting it in the STL container.
My questions are: What STL container should be used for the inventory(fixed sized)?
I have a pile of data, which i need to access frequently and fast. One entry consists of two unsigned ints, let`s call them source and destination.
One source can have several destinations, but this rarely ever happens. Several sources can have the same destination - this happens more frequently, but still scarcely.
Once the data is set up, it rarely ever changes - yet it should be possible to add or remove entries.
I now need to find all sources for a given destination, or all destinations for a given source.
The question: which stl container to choose?
I have tried a multimap, using the source as key. This works good for finding all d for a given s, but is inefficient in the other direction.
Do you think it would be more efficient to have two multimaps, sorted respectively by source and destination?
I am trying to come up with a way to make use of a "multilevel dynamic" container. I am parsing a file to grab some pieces of data. Lets say the first field of data I find I push into an array. At the same time lets I wish to create 2 cascaded sublevels. So an element in Modules is a pointer to the Types vector associated with that module and each element in Types is a pointer to a vector of Data. This concept should be similar to memory paging.
Obviously this becomes very hair quickly so it is obvious that I need to dynamically create and destroy vectors (if I do it this way). Should I just create pointers using the new operator?
Here is some of my code if it is even worthwhile to read:
If you take the first one, "max_logvar" is a module so everything between < and > is associated with that module.
symb is unimportant for now.
then "proterm" is a "module type" so then module now needs a module type container but I may have more than one of those.
so then I break it down by "Input" and "Output" where each of those can have the integer values (just in an array where each position will be set) that are in the fields to the right.
How do we design a container of objects all of which belong to some subclass which directly/indirectly inherits from a given parent class? Moreover, I would like to have functions that enable me to pick only objects of a certain class type from the container.
For example if the parent class is A and I have a hierarchy of classes that derive from it, we must have a container that can contain any class that exists in this hierarchy. Also, get_B() must be able to let me examine only those objects in this container that inherit (directly/indirectly) from class B (class B exists in the hierarchy rooted at A).
Preferably, we would like to avoid downcasting. Or even explicit typechecking of any sort.
I need to create an array container with the same structure as double myA [100][100];. But I cannot declare it as array<double, 100, 100> myA; however I do need this container for its format. How can I make it work.
i built a programming language called jade that right now can only print. i want to add variables to it however. I am going to use a modified bajarne stroustrop calculator to handle expressions (ie will now include string manipulations and such), but I want to build a var class into the program to make it easier for the program. i want variables to act like python variabes, and arrays to act like python associative arrays. Ive scoured different containers, but they only work if the variable isnt an array in my language, because it will only have one type. the only thing i can come up with is a union and 4 overloaded = operators (for bool, int, double, and string) is there a better way to do this?
Lexer.cpp: In member function 'void Lexer::PrintSource()': Lexer.cpp:29:42: error: 'struct std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > >' has no member named 'begin' for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++) ^ Lexer.cpp:29:76: error: 'struct std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > >' has no member named 'end' for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++) ^
I want to find all instances of a substring mysub in array container myarr and replace each occurrence of mysub with empty string " ". To do that, I'd like to use for loop with search algorithm.
Code below:
p=array iterator for ( p=search(myarr.cbegin(),myarr.end(), mysub.begin(),mysub.end(); p!=myarr.end(); p=search(p,myarr.end(),mysub.begin(),mysub.end() ); {
I need a "meaningful" way of accessing a table, the column is representing Err magnitude, and the row is representing Rate magnitude. For each error magnitude and rate magnitude, i define an action magnitude, which is the contains of the table. For example,
Code: int matrix[10][10]; int Action1 = matrix[0][0]; int Action2 = matrix[0][1];
However, i need a better way of getting matrix[0][0], row and col itself is meaningless. I want to access the table like
"Action magnitude" = matrix["Rate magnitude 1"]["Err magnitude 2"]; using a string instead of int id.
How do you find, when you first introduced with say some of the STL container, function or algorithms, what are its requirements? I mean,
1. what operators object should have for specific container? 2. what function args and type it should return for a algo? 3. when extending some container like with custom allocator, char_traits...and what not, what methods should be overridden? What work should they do?
When using an iterator with a std container (list / vector etc) sometimes it's possible to modify the container (e.g. delete an item) yet still carry on using the iterator - whereas in other cases, modifying the container immediately invalidates any open iterators on it. Is there an easy way to know which containers fall into which category? (or does it vary from one compiler to another?)