C++ :: Std Container Iterators - Category?
Jun 15, 2014
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?)
View 2 Replies
ADVERTISEMENT
Jul 5, 2012
I'm working on writing some classes around a ROM hardware addon card. The classes expose the data on the ROM as a container with iterators, much like a vector or a list.
The classes don't have any data themselves, since all the data is on the ROM.
I'm having some dillemma's as to how to approach/implement the classes. If you were to write somethign like this... Or were using something like this written by someone else.... How would you expect this to be done ?
1) Make all the member functions static, make a private constructor to prevent making instances. This works, but may look a bit weird...
Code:
for (auto it = RomTable::begin(); it != RomTable::end(); ++it)
2) expect users to make a (dummy) instance, then use it as a regular container. this might be a bit counter intuitive since the class has no datamembers.
3) create a single instance, expect users to use that everywhere. make the constructor inaccessible. Some C++ 'purists' might perceive this as global data and thus not a good solution ?
Additionally. Do I need to provide both a const_iterator and an iterator ? There's nothing to be modified, so I'm guessing an iterator isn't needed (?) Or will some STL stuff not work without an iterator ? I'm obviously not fussed about the STL functions that make changes to the container to not work (like sort, fill, swap...)
View 14 Replies
View Related
Mar 14, 2013
I have a class that I'm going to use to store a category. Right now there are seven options, but there is the potential for a whole lot more in the future. So I started off by storing an integer as the private member. I then used static constants to define the numeric values to represent each category, then a set of static constant strings that corresponds to those numbers in case I need their actual names. Finally I set up some static functions to convert between the integer value and the string, and vice versa.
I'm not sure if this is the best way to go about this. For one it makes the categories names and designations unchangeable. I thought that storing them in a file would be a better option, but then i needed a container that is the equivalent of a constant.
I thought of defining a class to contain an int and the associated string. It would be designed so that it can only be initialized with both items. Then provide no functionality to change the contents. So I've basically created my own constant.
View 4 Replies
View Related
Jan 15, 2015
Given a product category and subcategory representation:
a. Come up with a tree data structure to minimally (in terms of storage) represent this
b. Write a program to convert the given representation (shown in example below) to this
c. Write a function to output the tree structure in a nice human readable format
Note:
a. There can be any number of levels of depth
b. Rows may be repeated in input, but need to feature only once in the final tree.
Example category list (read this input from a file):
everything else|office supplies
electronics|video games
electronics|video games|accessories
electronics|video games|accessories|headsets
electronics|video games|accessories|flight controls
electronics|video games|accessories|joysticks
View 5 Replies
View Related
Dec 25, 2014
I'm having problem getting the values for iterators. This code:
auto sum = inputVec.begin() + itLast;
gave this error: no match for 'operator+'
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main(){
vector<int> inputVec;
[Code] .....
View 4 Replies
View Related
Feb 6, 2013
Do stream iterators, such as std::istreambuf_iterator<char>, read a chunk of bytes internally, or do they read the stream one byte at a time?
Because I am thinking to write a BufferedFile class which uses an std::vector<char>.
View 3 Replies
View Related
May 20, 2013
In this code:
vector<int> mydata(100);
mydata[2] = 999;
In statement 2 does that call the iterator to access the 3rd position and set its value to 999?
View 18 Replies
View Related
Oct 13, 2013
Assuming I have a list of pointers to a generic type T:
#include <vector>
//...
list<T*> myList;
Now assuming I want to go on the list, and if T's type is matched to the type I'm looking for, then cast it to this type and do something. List shown here:
list<T*>:: const_iterator iter= myList.begin();
for(; iter!=myList.end(); ++iter){
if( typeid(*iter)==typeid(Something*)) //RUN-TIME ERROR
dynamic_cast<Something*>(*iter)->DoSomething();
}
how do I fix this run-time error?
View 1 Replies
View Related
Oct 8, 2014
I am working on very large code.
View 4 Replies
View Related
Oct 9, 2014
I am working on very large code. I got a segmentation fault when trying to use one cpp file and tried to locate the error using Valgrind
Since the code is very large, I will only post a short portion of it below. I think the problem may come because triann is a vector defined in the header class, so triann[tri] is causing problems?
void ADe::aNe(int v, set<int> &nei)
{
for(set<int>::iterator iter = vert2tri[v].begin(); iter != vert2tri[v].end(); iter++)
{
[Code].....
View 4 Replies
View Related
Jul 18, 2012
So i made an STL compatible container.And to make this work I had to make my own iterator (derived from std::iterator).
What is the portable (if any) and "well behaved" thing to do in case of usage anomalies.such as iterating an iterator too far, or passing an invalid index to a operator[]
Looking at how VC++ does things in something like std::array or std::vector.
Code:
iterator_type& operator+=(difference_type offset)
{// increment by integer
#if _ITERATOR_DEBUG_LEVEL == 2
if (size < index + offset)
{// report error
[Code] .....
lots of names starting with underscores, so it's implementation specific. Is there even a "well behaved" thing to do ? Or is any such work always going to be compiler specific?
View 2 Replies
View Related
May 29, 2013
I have a 'Graph' class, which has derived classes for Adjacency Matrix and Adjacency List representations.
How do I provide iterators for traversing vertices and edges, when the iterator classes would have different implementations for the different derived classes ?
The following way is the only one I can think of, but seems quite cumbersome.
Code:
class Base {
public:
class BaseIterator {
};
virtual const BaseIterator& begin();
virtual const BaseIterator& end();
[Code] .....
Or is there a pattern for doing this that I'm not aware of ? Would composition be a better idea here compared to polymorphism ? I mean, I can think like..a Graph can 'have' several representation 'objects' within it.
All the involved classes are templates,not sure if that makes the situation different.
View 7 Replies
View Related
Jul 11, 2013
I get this error when i try to run this code for an inventory in debug mode in VS. But for some reason it works just fine in release mode.
void Push_Back_Item(Item *item){
for(int y = 0; y < InvSizeY; y ++)
for(int x = 0; x < InvSizeX; x ++){
auto Iter = ItemList.find(std::make_pair(x,y));
if(Iter != ItemList.end()){
item->SetDead(); // ERROR
}
}
}
This isnt the full code though but it still gives me the same error.
The only thing "item->SetDead()" does is to set a bool to true.
This is the map i get the iterator from
std::map<std::pair<int,int>,Item*> ItemList;
This have been bugging me for quite some time now.
View 4 Replies
View Related
Mar 27, 2014
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)?
View 2 Replies
View Related
Sep 22, 2013
What is container class? what its advantage ?
View 2 Replies
View Related
Sep 16, 2012
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?
View 1 Replies
View Related
Mar 6, 2013
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.
/*
Modules Types Data
____ _____ ______
|____|------> |_____| ---------->|______|
|____| |_____| |______|
|____| |_____| |______|
|____|
|____|
|____| _____ ______
|____|------> |_____|----------->|______|
|____| |_____| |______|
*/
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:
class Parser {
//storage arrays
vector <string> modules;
vector <string> terminal_type;
vector <string> tag_type;
vector <string> data;
[Code] ....
parse this stuff and nicely organize it.
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.
View 1 Replies
View Related
Nov 21, 2013
I have a container in c++11 and i must delete first object and i don't know how...
for (const prob& p : *static_cast<vector<prob>*>(list.listData)){
// i must delete the first object
}
View 2 Replies
View Related
Apr 25, 2014
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.
View 8 Replies
View Related
Mar 30, 2014
To my understanding, a Container should have an Allocator member data, which is used internally.
Question: what happens, for example, when we swap() two containers that use different allocators?
Must the allocators be swapped as well?
Must all elements of the container be reallocated when the allocator is changed?
View 7 Replies
View Related
Jun 24, 2013
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.
View 6 Replies
View Related
Feb 9, 2013
I know that it's a class that contains data from another class I just do not know how to make them both work together.
View 1 Replies
View Related
Jun 14, 2013
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?
View 2 Replies
View Related
Oct 31, 2013
so i have a container called Source that is of type std::map<std::string, std::vector<std::string>> but when I try to print it with this function:
void Lexer::PrintSource()
{
for(auto Iterator = this->Source.begin(); Iterator != this->Source.end(); Iterator++)
for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++)
cout<< *SubIterator << endl;
}
i get these errors:
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++)
^
View 6 Replies
View Related
Nov 23, 2013
I have a deposit of containers and i must delete the cats container. How i can do that?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Cat {
string name;
int age;
[Code] .....
View 2 Replies
View Related
May 13, 2014
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() ); {
[Code] ......
View 2 Replies
View Related