Search Results

Search found 1006 results on 41 pages for 'istream iterator'.

Page 4/41 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Is there a linear-time performance guarantee with using an Iterator?

    - by polygenelubricants
    If all that you're doing is a simple one-pass iteration (i.e. only hasNext() and next(), no remove()), are you guaranteed linear time performance and/or amortized constant cost per operation? Is this specified in the Iterator contract anywhere? Are there data structures/Java Collection which cannot be iterated in linear time? java.util.Scanner implements Iterator<String>. A Scanner is hardly a data structure (e.g. remove() makes absolutely no sense). Is this considered a design blunder? Is something like PrimeGenerator implements Iterator<Integer> considered bad design, or is this exactly what Iterator is for? (hasNext() always returns true, next() computes the next number on demand, remove() makes no sense). Similarly, would it have made sense for java.util.Random implements Iterator<Double>?

    Read the article

  • How to remove an element from set using Iterator?

    - by ankit
    I have a scenario that I am iterating over a set using iterator. Now I want to remove 1st element while my iterator is on 2nd element. How can I do it. I dont want to convert this set to list and using listIterator. I dont want to collect all objects to be removed in other set and call remove all sample code. Set<MyObject> mySet = new HashSet<MyObject>(); mySet.add(MyObject1); mySet.add(MyObject2); ... Iterator itr = mySet.iterator(); while(itr.hasNext()) { // Now iterator is at second element and I want to remove first element }

    Read the article

  • Const-correctness semantics in C++

    - by thirtythreeforty
    For fun and profit™, I'm writing a trie class in C++ (using the C++11 standard.) My trie<T> has an iterator, trie<T>::iterator. (They're all actually functionally const_iterators, because you cannot modify a trie's value_type.) The iterator's class declaration looks partially like this: template<typename T> class trie<T>::iterator : public std::iterator<std::bidirectional_iterator_tag, T> { friend class trie<T>; struct state { state(const trie<T>* const node, const typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator& node_map_it ) : node{node}, node_map_it{node_map_it} {} // This pointer is to const data: const trie<T>* node; typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator node_map_it; }; public: typedef const T value_type; iterator() =default; iterator(const trie<T>* node) { parents.emplace(node, node->children.cbegin()); // ... } // ... private: std::stack<state> parents; // ... }; Notice that the node pointer is declared const. This is because (in my mind) the iterator should not be modifying the node that it points to; it is just an iterator. Now, elsewhere in my main trie<T> class, I have an erase function that has a common STL signature--it takes an iterator to data to erase (and returns an iterator to the next object). template<typename T> typename trie<T>::iterator trie<T>::erase(const_iterator it) { // ... // Cannot modify a const object! it.parents.top().node->is_leaf = false; // ... } The compiler complains because the node pointer is read-only! The erase function definitely should modify the trie that the iterator points to, even though the iterator shouldn't. So, I have two questions: Should iterator's constructors be public? trie<T> has the necessary begin() and end() members, and of course trie<T>::iterator and trie<T> are mutual friends, but I don't know what the convention is. Making them private would solve a lot of the angst I'm having about removing the const "promise" from the iterator's constructor. What are the correct const semantics/conventions regarding the iterator and its node pointer here? Nobody has ever explained this to me, and I can't find any tutorials or articles on the Web. This is probably the more important question, but it does require a good deal of planning and proper implementation. I suppose it could be circumvented by just implementing 1, but it's the principle of the thing!

    Read the article

  • How might I wrap the FindXFile-style APIs to the STL-style Iterator Pattern in C++?

    - by BillyONeal
    Hello everyone :) I'm working on wrapping up the ugly innards of the FindFirstFile/FindNextFile loop (though my question applies to other similar APIs, such as RegEnumKeyEx or RegEnumValue, etc.) inside iterators that work in a manner similar to the Standard Template Library's istream_iterators. I have two problems here. The first is with the termination condition of most "foreach" style loops. STL style iterators typically use operator!= inside the exit condition of the for, i.e. std::vector<int> test; for(std::vector<int>::iterator it = test.begin(); it != test.end(); it++) { //Do stuff } My problem is I'm unsure how to implement operator!= with such a directory enumeration, because I do not know when the enumeration is complete until I've actually finished with it. I have sort of a hack together solution in place now that enumerates the entire directory at once, where each iterator simply tracks a reference counted vector, but this seems like a kludge which can be done a better way. The second problem I have is that there are multiple pieces of data returned by the FindXFile APIs. For that reason, there's no obvious way to overload operator* as required for iterator semantics. When I overload that item, do I return the file name? The size? The modified date? How might I convey the multiple pieces of data to which such an iterator must refer to later in an ideomatic way? I've tried ripping off the C# style MoveNext design but I'm concerned about not following the standard idioms here. class SomeIterator { public: bool next(); //Advances the iterator and returns true if successful, false if the iterator is at the end. std::wstring fileName() const; //other kinds of data.... }; EDIT: And the caller would look like: SomeIterator x = ??; //Construct somehow while(x.next()) { //Do stuff } Thanks! Billy3

    Read the article

  • C++ Iterator lifetime and detecting invalidation

    - by DK.
    Based on what's considered idiomatic in C++11: should an iterator into a custom container survive the container itself being destroyed? should it be possible to detect when an iterator becomes invalidated? are the above conditional on "debug builds" in practice? Details: I've recently been brushing up on my C++ and learning my way around C++11. As part of that, I've been writing an idiomatic wrapper around the uriparser library. Part of this is wrapping the linked list representation of parsed path components. I'm looking for advice on what's idiomatic for containers. One thing that worries me, coming most recently from garbage-collected languages, is ensuring that random objects don't just go disappearing on users if they make a mistake regarding lifetimes. To account for this, both the PathList container and its iterators keep a shared_ptr to the actual internal state object. This ensures that as long as anything pointing into that data exists, so does the data. However, looking at the STL (and lots of searching), it doesn't look like C++ containers guarantee this. I have this horrible suspicion that the expectation is to just let containers be destroyed, invalidating any iterators along with it. std::vector certainly seems to let iterators get invalidated and still (incorrectly) function. What I want to know is: what is expected from "good"/idiomatic C++11 code? Given the shiny new smart pointers, it seems kind of strange that STL allows you to easily blow your legs off by accidentally leaking an iterator. Is using shared_ptr to the backing data an unnecessary inefficiency, a good idea for debugging or something expected that STL just doesn't do? (I'm hoping that grounding this to "idiomatic C++11" avoids charges of subjectivity...)

    Read the article

  • Why can't I sort this container?

    - by Knowing me knowing you
    Please don't mind that there is no insert fnc and that data are hardcoded. The main purpouse of it is to correctly implement iterator for this container. //file Set.h #pragma once template<class T> class Set { template<class T> friend ostream& operator<<(ostream& out, const Set<T>& obj); private: T** myData_; std::size_t mySize_; std::size_t myIndex_; public: Set(); class iterator : public std::iterator<std::random_access_iterator_tag, T*> { private: T** itData_; public: iterator(T** obj) { itData_ = obj; } T operator*() const { return **itData_; } /*Comparing values of two iterators*/ bool operator<(const iterator& obj) { return **itData_ < **obj.itData_; } /*Substracting two iterators*/ difference_type operator-(const iterator& obj) { return itData_ - obj.itData_; } /*Moving iterator backward for value*/ iterator operator-(const int value) { return itData_ - value; } /*Adding two iterators*/ difference_type operator+(const iterator& obj) { return itData_ + obj.itData_; } /*Moving iterator forward for value*/ iterator operator+(const int value) { return itData_ + value; } bool operator!=(const iterator& obj) { return (itData_ != obj.itData_); } bool operator==(const iterator& obj) { return (itData_ == obj.itData_); } T** operator++() { return ++itData_; } T** operator--() { return --itData_; } }; iterator begin() const { return myData_; } iterator end() const { return myData_ + myIndex_; } }; template<class T> ostream& operator<<(ostream& out, const Set<T>& obj) { for (int i = 0;i < 3; ++i) { out << *obj.myData_[i] << "\n"; } return out; } //file Set_impl.h #pragma once #include "stdafx.h" #include "Set.h" template<class T> Set<T>::Set() { mySize_ = 3; myIndex_ = 3; myData_ = new T*[mySize_]; myData_[0] = new T(3); myData_[1] = new T(1); myData_[2] = new T(2); } //main include "stdafx.h" #include "Set_impl.h" int _tmain(int argc, _TCHAR* argv[]) { Set<int> a; Set<int>::iterator beg_ = a.begin(); Set<int>::iterator end_ = a.end(); std::sort(beg_,end_);//WONT SORT THIS RANGE cin.get(); return 0; } Why sort can't accept this iterators even though I've provided all operators needed for sort to work? I think the best way to check what's going on is to paste this code and run it first. Thanks

    Read the article

  • In which DLL is the COM interface iStream defined?

    - by Youval Bronicki
    I'm a complete newbie to Windows and COM programming, trying to use com4j in order to call a COM object from Java. Com4j generates Java interfaces from COM definitions "often found in .ocx, .dll, .exe, and/or .tlb files" . It was easy for me to locate the .ocx file of my target COM object, but I have no clue regarding the standard interface iStream. Microsoft's documentation mentions OLE32.DLL ( c:\Windows\Windows32\Ole32.dll ?) , but neither the com4j generator nor oleviewer succeed in opening this file. Any hints?

    Read the article

  • Is it a good idea to create an STL iterator which is noncopyable?

    - by BillyONeal
    Most of the time, STL iterators are CopyConstructable, because several STL algorithms require this to improve performance, such as std::sort. However, I've been working on a pet project to wrap the FindXFile API (previously asked about), but the problem is it's impossible to implement a copyable iterator around this API. A find handle cannot be duplicated by any means -- DuplicateHandle specifically forbids passing handles to it. And if you just maintain a reference count to the find handle, then a single increment by any copy results in an increment of all copies -- clearly that is not what a copy constructed iterator is supposed to do. Since I can't satisfy the traditional copy constructible requirement for iterators here, is it even worth trying to create an "STL style" iterator? On one hand, creating some other enumeration method is going to not fall into normal STL conventions, but on the other, following STL conventions are going to confuse users of this iterator if they try to CopyConstruct it later. Which is the lesser of two evils?

    Read the article

  • std::deque: How do I get an iterator pointing to the element at a specified index?

    - by Ptah- Opener of the Mouth
    I have a std::deque, and I want to insert an element at a specified index (I'm aware that std::list would be better at this). The deque::insert() function takes an iterator to specify the location to insert. Given an index, how can I get an iterator pointing to that location, so that I can pass that iterator to insert()? For example: void insertThing ( deque<Thing> & things, Thing thing, size_t index ) { deque<Thing>::iterator it = /* what do I do here? */ things.insert ( it, thing ); } I'm sure this is a very basic question, and I apologize for it. It's been a long time since I've used the STL, and I don't see anything in std::deque's member list that obviously does what I want. Thanks.

    Read the article

  • Writing a custom iterator -- what to do if you're at the end of the array?

    - by Goose Bumper
    I'm writing a custom iterator for a Matrix class, and I want to implement the increment method, which gets called when the iterator is incremented: void MatrixIterator::increment() { // go to the next element } Suppose the iterator has been incremented too many times and now points to past the end of the matrix (i.e. past the one-past-the-end point). What is the best practice for this situation? Should I catch this with an assert, or should I just say it's the user's responsibility to keep track of where the iterator is pointing and it's none of my business?

    Read the article

  • Why does string::find return size_type and not an iterator?

    - by dehmann
    In C++, why does string::find return size_type and not an iterator? It would make sense because functions like string::replace or string::insert take iterators as input, so you could find some character and immediately pass the returned iterator to replace, etc. Also, std::find returns an iterator -- why is std::string::find different?

    Read the article

  • ServiceLoader double iterator issues

    - by buge
    Is this a known issue? I had trouble finding any search results. When iterating over a ServiceLoader while an iteration already is in progress, the first iteration will be aborted. For example, assuming there are at least two implementations of Foo, the following code will fail with an AssertionError: ServiceLoader<Foo> loader = ServiceLoader.load(Foo.class); Iterator<Foo> iter1 = loader.iterator(); iter1.next(); Iterator<Foo> iter2 = loader.iterator(); while (iter2.hasNext()) { iter2.next(); } assert iter1.hasNext(); This only seems to occur, if the second iterator really terminates. The code will succeed in this variation for example: ServiceLoader<Foo> loader = ServiceLoader.load(Foo.class); Iterator<Foo> iter1 = loader.iterator(); iter1.next(); Iterator<Foo> iter2 = loader.iterator(); iter2.next(); assert iter1.hasNext(); Is this a bug or a feature? :p Is there a ticket for this already anywhere?

    Read the article

  • Iterator in Java.

    - by theband
    What is Iterator and collections? Does these two have any relations? // the interface definition Interface Iterator { boolean hasNext(); Object next(); // note "one-way" traffic void remove(); } // an example public static void main (String[] args){ ArrayList cars = new ArrayList(); for (int i = 0; i < 12; i++) cars.add (new Car()); Iterator it = cats.iterator(); while (it.hasNext()) System.out.println ((Car)it.next()); } Does the Interface Iterator has these method names alone predefined or its user defined?. What does these four lines below actually tell? cars.add (new Car()); Iterator it = cats.iterator(); while (it.hasNext()) System.out.println ((Car)it.next()); Thanks i am going through a book in collections.

    Read the article

  • IWebBrowser: How to specify the encoding when loading html from a stream?

    - by Ian Boyd
    Using the concepts from the sample code provided by Microsoft for loading HTML content into an IWebBrowser from an IStream using the web browser's IPersistStreamInit interface: HRESULT LoadWebBrowserFromStream(IWebBrowser* pWebBrowser, IStream* pStream) { [snip] } How can one specify the encoding of the html inside the IStream? The IStream will contain a series of bytes, but the problem is what do those bytes represent? They could, for example, contain bytes where: each byte represents a character from the current Windows code-page (e.g. 1252) each byte could represent a character from the ISO-8859-1 character set the bytes could represent UTF-8 encoded characters every 2 bytes could represent a character, using UTF-16 encoding In my particular case, i am providing the IWebBrowser an IStream that contains a series of double-bytes characters (UTF-16), but the browser (incorrectly) believes that UTF-8 encoding is in effect. This results in garbled characters.

    Read the article

  • Writing my own implementation of stl-like Iterator in C++.

    - by Negai
    Good evening everybody, I'm currently trying to understand the intrinsics of iterators in various languages i.e. the way they are implemented. For example, there is the following class exposing the list interface. template<class T> class List { public: virtual void Insert( int beforeIndex, const T item ) throw( ListException ) =0 ; virtual void Append( const T item ) =0; virtual T Get( int position ) const throw( ListException ) =0; virtual int GetLength() const =0; virtual void Remove( int position ) throw( ListException ) =0; virtual ~List() =0 {}; }; According to GoF, the best way to implement an iterator that can support different kinds of traversal is to create the base Iterator class (friend of List) with protected methods that can access List's members. The concrete implementations of Iterator will handle the job in different ways and access List's private and protected data through the base interface. From here forth things are getting confusing. Say, I have class LinkedList and ArrayList, both derived from List, and there are also corresponding iterators, each of the classes returns. How can I implement LinkedListIterator? I'm absolutely out of ideas. And what kind of data can the base iterator class retrieve from the List (which is a mere interface, while the implementations of all the derived classes differ significantly) ? Sorry for so much clutter. Thanks.

    Read the article

  • DVD burn IStream data

    - by sijith
    i want to write single directory into my DVD. How can i pass data in form of IStream. m_hResult = m_discFormatData->Write(m_streamData); How to show my directory to IStream Please help

    Read the article

  • $stdin compatibility with std::istream using swig, C++, and Ruby

    - by Kenny Peng
    I have a function in C++ that takes in an std::istream as the input: class Foo { Foo(std::istream &); } Using SWIG, I've bound it to Ruby, but Ruby's $stdin variable is fundamentally different from anything like the stream classes in C++, so I'm not sure how to either 1) expose the C++ class to Ruby in a way that I can use $stdin, or 2) convert $stdin into something the C++ class can understand. Anyone have experience with binding iostreams in C++ to Ruby? Thanks.

    Read the article

  • libgdx - #iterator() cannot be used nested

    - by TimSim
    I'm getting this error when I try to check if any of the targets overlap each other: iterTargets = targets.iterator(); while (iterTargets.hasNext()) { Target target = iterTargets.next(); for (Target otherTarget:targets) { if (target.rectangle.overlaps(otherTarget.rectangle)) { // do something } } } So I can't do that? How am I supposed to check each member of an array to see if it overlaps any other member?

    Read the article

  • pushing back an boost::ptr_vector<...>::iterator in another boost::ptr_vector?

    - by Ethan Nash
    Hi all, I have the following code (just typed it in here, might have typos or stuff): typedef boost::ptr_vector<SomeClass> tvec; tvec v; // ... fill v ... tvec vsnap; for(tvec::iterator it = v.begin(); it != v.end(); ++it) { if((*v).anyCondition) vsnap.push_back( it ); // (*it) or &(*it) doesn't work } My problem is now that i cant push_back an iterator in any way, I just don't get the pointer out of the iterator. Is there an easy way i didnt see, or are boosts ptr_vector the false choice for this case? Thanks in advance.

    Read the article

  • How to get the number of loop when using an iterator, in C++?

    - by pollux
    Dear reader, I'm working on a aplication where I draw a couple of images, like this: void TimeSlice::draw(float fX, float fY) { list<TimeSliceLevel*>::iterator it = levels.begin(); float level_x = x; float level_y = y; while(it != levels.end()) { (*it)->draw(level_x,level_y); level_y += (*it)->height; ++it; } } Though this is a bit incorrect. I need to position the TimeSliceLevel* on a X.. When I've got a for(int i = 0; i < slices.size(); ++i) loop, I can use x = i * width. Though I'm using an iterator as I've been told many times that's good programming : and I'm wondering if the iterator has a "index" number of something which I can use to calculate the new X position? (So it's more a question about using iterators) Kind regards, Pollux

    Read the article

  • Why only random-access-iterator implements operator+ in C++?

    - by xopht
    I'd like get far next value for STL list iterator but it doesn't implement operator+, vector has it though. Why and how can I get the value where I want? I think I can do that if I call operator++ several times, but isn't that a little bit dirty? What I want to do is the following: list<int> l; ...omitted... list<int>::iterator itr = l.begin() + 3; // but, list iterator does not have // operator+ What is the best solution for what I want?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >