template<class RanAccIt>
void some_algorithm( RanAccIt begin, RanAccIt end)
{
// this algorithm involves calling std::lexicograp hical_compare
// on range [begin, end), and on reverse of this range
// (i.e. as rbegin, rend was passed)
}
How can I call lexicographical _compare inside the function so that it
traverses the range backwards?
The ideal would be to somehow make reverse iterators from normal iterators,
but I can't see a way to do it (even though the iterators are random access,
so backwards iteration is fully supported).
The only way I see is to pass reverse iterators to the function in addition
to normal iterators. I don't want to do that, because:
(1) it is redundant - begin/end define the range perfectly
(2) it reveals internals of the algorithm to the outside - one day I will
come with an implementation that does not need reverse iterators, and I will
have to change function signature
Is there other way?
Also, if I wanted to hand-code a loop over reverse of range [begin, end),
would the below be conforming:
template<class RanAccIt>
void some_algorithm( RanAccIt begin, RanAccIt end)
{
RanAccIt rbegin = end - 1;
RanAccIt rend = begin - 1;
for (RanAccIt it = rbegin; it != rend; --it)
{
/* ... */
}
}
cheers,
M. 1 1900
Marcin Kaliciñski wrote: template<class RanAccIt> void some_algorithm( RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicograp hical_compare // on range [begin, end), and on reverse of this range // (i.e. as rbegin, rend was passed) }
How can I call lexicographical _compare inside the function so that it traverses the range backwards?
The ideal would be to somehow make reverse iterators from normal iterators, but I can't see a way to do it (even though the iterators are random access, so backwards iteration is fully supported).
The only way I see is to pass reverse iterators to the function in addition to normal iterators. I don't want to do that, because: (1) it is redundant - begin/end define the range perfectly (2) it reveals internals of the algorithm to the outside - one day I will come with an implementation that does not need reverse iterators, and I will have to change function signature
Is there other way?
You want to have a look at std::reverse_it erator:
#include <iterator>
#include <iostream>
template <typename RndAccIt>
void print_reversed_ range ( std::ostream & out,
RndAccIt const & from,
RndAccIt const & to ) {
for ( std::reverse_it erator< RndAccIt > iter ( to );
iter != std::reverse_it erator< RndAccIt >( from );
++ iter ) {
out << *iter << ' ';
}
}
#include <vector>
int main ( void ) {
std::vector< int > i_vect;
for ( int i = 0; i < 10; ++i ) {
i_vect.push_bac k( i );
}
print_reversed_ range( std::cout, i_vect.begin(), i_vect.end() );
}
Also, if I wanted to hand-code a loop over reverse of range [begin, end), would the below be conforming:
template<class RanAccIt> void some_algorithm( RanAccIt begin, RanAccIt end) { RanAccIt rbegin = end - 1; RanAccIt rend = begin - 1;
Ouch, the before-begin position is in general not a valid thing.
for (RanAccIt it = rbegin; it != rend; --it) { /* ... */ } }
cheers, M.
Best
Kai-Uwe Bux This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Stippler |
last post by:
Hello,
is it reasonable, that most stl-implementations allow conversion from
iterator to const_iterator via conversion constructors like the following?
template<class _Other>
reverse_iterator(const reverse_iterator<_Other>& _Right);
This signature unfortunately allows the other direction, wich is
semantically surely not the desired thing. GCC for example uses this way
|
by: Lasse Vågsæther Karlsen |
last post by:
I need to merge several sources of values into one stream of values. All
of the sources are sorted already and I need to retrieve the values from
them all in sorted order.
In other words:
s1 =
s2 =
s3 =
for value in ???(s1, s2, s3):
|
by: Jiri Kripac |
last post by:
Languages such as Simula 67 contain a general concept of coroutines that
allow the execution of a method to be suspended without rolling back the
stack and then later resumed at the same place as it has been suspended.
The C# iterators seem to be a special case of this general suspend/resume
concept. The "yield" statement suspends the execution of the current method
and calling MoveNext() resumes it.
I think it would be cleaner to...
|
by: Shiraz |
last post by:
Me and my colleagues have been working on a problem since yesterday and can’t
seem to find a solution to it. It relates in a good measure to iterators and
the way we’re using them in our project. I will first give you a little
introduction to our main task. We had at our hands entire modules which were
written in C++. These were to communicate with a stack written in C, and in
turn act as a layer between the C code and C# code used in...
|
by: ma740988 |
last post by:
typedef std::vector < std::complex < double > > complex_vec_type;
// option1
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( &cc, &cc );
}
versus
| |
by: zefciu |
last post by:
In the tutorial there is an example iterator class that revesrses the
string given to the constructor. The problem is that this class works
only once, unlike built-in types like string. How to modify it that it
could work several times? I have tried two approaches. They both work,
but which of them is stylistically better?
class Reverse: #original one
"Iterator for looping over a sequence backwards"
def __init__(self, data):...
|
by: emma_middlebrook |
last post by:
Hi
Another query relating to iterators.
The following seems to work but it's different from the examples I can
find on MSDN ...
I'm trying to retrofit iterators into a non-generic class - here's an
attempt to make a forward and reverse one, with forward being the
default ...
|
by: desktop |
last post by:
1)
I have this code:
std::list<intmylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
|
by: Michael Coley |
last post by:
Hi, I've wrote this function which should add a comma for every 3
digits in a number (so that it looks something like 5,000).
This is my function:
std::string formatNumber(int number)
{
// Convert the int to a string.
std::string numString = boost::lexical_cast<std::string>(number);
std::string::const_reverse_iterator i = numString.rbegin(), end =
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |