C++ forward iterator arithmetic - c++

Below is a simple program to print the position within a vector of a small range of ints.
int mynumbers [] = { 3, 9, 0, 2, 1, 4, 5 };
vector<int> v (mynumbers, mynumbers + 7);
int m1[] = {9, 0, 2 };
vector<int>::iterator it = find_end (v.begin(), v.end(), m1, m1+3);
if (it != v.end())
cout << "Found at position: " << it-v.begin() << endl;//LINE I
The program as expected prints
Found at position: 1
the find_end function returns a forward iterator.
Logically it makes no sense for me to do so but when I change 'LINE I' to
cout << "Found at position: " << it+v.begin() << endl;//LINE I
I get a compile error.
I know I should be using 'distance' however I am just trying to familiarise myself with the limitations of the various iterator types
Thanks

it is an iterator, it represents a location where an object is.
You say "logically it makes no sense for me to do so..." about it+v.begin(), which is right, because you're adding two locations. What do you get when you add the location of Californa to the location of New York? The question literally makes no sense.
However, you can take the distance between two locations: it-v.begin(). Not sure exactly what you're asking, but I hopefully covered it.

The simple answer is that there is an operation defined for b - a, where b and a are of the same iterator types, but there is no operation defined for b + a.
Sources:
Non-normative: cppreference, RandomAccessIterator
Normative: N4140, [random.access.iterators] Table 111

You're on the right track.
But I think the distance that you mention in your question is your actual solution: http://www.cplusplus.com/reference/iterator/distance/
This is your situation:
begin() --> 3
it --> 9
0
2
1
4
5
distance(v.begin(), it) will tell you how far those two pointers are apart. (In this case 1).
Now keep in mind under the hood distance will just do the same subtraction that you do in this case.
How you should think about it is like this, v.begin() is an address and so is it.
v.begin() is 0x0100
it is 0x0104
when you subtract them you're going to get (0x0104 - 0x0100) / sizeof(int) so you'll get 1.

Related

Reverse Iterator not working as expected C++

Hey i was wondering why this code doesnt give 1 to output.
vector<int> myVector{1, 2, 3, 4, 6};
cout << *myVector.rend() << endl;
Output should be 1 but it gives random numbers.
But in this example everything is okay.
vector<int> myVector{1, 2, 3, 4, 6};
cout << *myVector.rbegin() << endl;
Output : 6
Thanks.
end() points to the memory location after the last element. Similarly, rend() points to memory location before the first element. They are supposed to be used as sentinel values ─ i.e. to iterate until that point is reached.
So, to print 1, you should use:
cout << *(myVector.rend()-1) << endl;
"end" iterators are really "past the end" and not dereferencable, no matter whether they're forward or reverse.
BTW: Enable diagnostic mode for you C++ stdlib. It would have told you something's wrong. How you do that depends on the compiler and stdlib.
In the same way that end() gives an invalid iterator "one item after the range", rend() gives an invalid iterator "one item before the range". So your first example outputs whatever semi-random data happens to be at that place in memory.

Why is this iterator arithmetic not working?

I am learning C++ for fun. I am trying to understand iterator arithmetic and have written a simple program that sums the first element in a vector with the last element. Next the second and second-to-last elements are summed. The process continues until all elements have been summed in this manner.
The issue seems to be with the *(a.end() - 1 - it) portion of my code. If I remove the - it, then I get the result I would expect. Adding the - it results in an error saying that
there were build errors. Would you like to continue to run the last successful build?
The errors I receive from Visual Studio are as follows;
E0075 Operand of * must be a pointer
C2100 illegal indirection
I have already approached this problem in a different way and was able to accomplish my goal. My question is why is this code an error, and could it be modified slightly to execute properly?
I appreciate any advice.
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> a{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto it = a.begin(); it != a.begin() + (a.end() - a.begin()) / 2; ++it)
cout << (*it) + *(a.end() - 1 - it) << endl;
return 0;
} // END MAIN
Error message
Error list
Subtracting 2 iterators with a.end() - 1 - it gives you the distance between them. You can't dereference a distance with * and so you get a compiler error.
This distance from a.begin() to the element you want to get at is needed in the expression. You can adjust the expression slightly by just adding a.begin() to that distance:
cout << (*it) + *(a.end() - 1 - it + a.begin()) << endl;
// ^^^^^^^^^^^
Here's a demo.
From C++20, instead of dealing with iterators and manipulating them, you can deal with ranges directly, which can be easier to read once you get used to it:
// just for convenience
namespace sv = std::views;
namespace sr = std::ranges;
// size of half the range
auto half = a.size() / 2;
sr::transform(a | sv::take(half), // first half
a | sv::reverse | sv::take(half), // second half reversed
std::ostream_iterator<int>(std::cout, "\n"), // print out
std::plus{}); // the sum
Here's a demo.
You should change code like this:
auto fix = it - a.begin();
cout << (*it) + *(a.end() - 1 - fix) << endl;
you can't manipulate on iterators like integers, you just can simply add or sub integers or use starting point from begin() - as end() isn't a real pointer for some value in vector.

How to tell the current 'write position' of a Boost Circular Buffer to enable accessing a previously stored value

I want to access a value in a Boost Circular Buffer that is, for example, 5 positions 'in the past'. So, imagine I am now writing a value '7' to a previous stream of integers:
3, 5, 6, 9, 2, 8, 6
Therefore I have now:
7, 3, 5, 6, 9, 2, 8, 6
I want the '2' since that is 5 positions in the past. How do I get that?
In other words, what is the current 'write index'?
I think that I might need to use boost::circular_buffer<double>::const_iterator but I'm not sure.
I'm not sure I understand correctly, but your worries about modulo indexing seem overly anxious to me. The whole purpose of a circular buffer abstraction is to hide the index arithmetics from the caller, if you ask me.
I'd be thoroughly disappointed in the library design if Boost would let this implementation detail leak out¹
Here's a simple demo that seems to be what you want:
Live On Coliru
#include <iostream>
#include <boost/circular_buffer.hpp>
int main() {
boost::circular_buffer<int> cb(10); // [tag:cb-so-fixedsize] obviously
for (int msg : { 3, 5, 6, 9, 2, 8, 6, 7 }) {
cb.push_back(msg);
}
// should be 2
std::cout << "t0-5: " << cb[4] << "\n";
std::cout << "t0-5: " << *std::next(cb.begin(), 4) << "\n";
// should be 9
std::cout << "t0-5: " << cb[3] << "\n";
std::cout << "t0-5: " << *std::next(cb.begin(), 3) << "\n";
while (!cb.empty()) {
std::cout << cb.front() << " ";
cb.pop_front();
}
}
Prints
t0-5: 2
t0-5: 2
t0-5: 9
t0-5: 9
3 5 6 9 2 8 6 7
¹ I'm well aware that the implementation detail is implied by the name "circular", but hey. Many data structures in the standard library and alike have slightly confusing names
I think the only viable answer is for you to keep track of the index yourself. According to the Boost documentation, it appears that the container has a fixed allocation size and works similar to a stack.
Like other containers, elements do not intrinsically keep track of their key. Therefore, it is up to you to externally keep track of any properties like position or key. It appears that you only have the usual std::vector-like functions available to you: size(), capacity(), at(), operator[], and such, including the iterator functions.
I suppose that the best thing you can do is to find out some way of keeping track of the "keys" or index numbers. Since the circular buffer simply loops around and rewrites over the first array on "overflow," perhaps you could keep a count yourself with a modulo function.

lower_bound == upper_bound

What does lower_bound mean. If I had to guess I would answer that this function returns the iterator at the last element that is less than the value asked for. But I see that lower_bound is almost the same as upper_bound. The only difference is strict inequality in the case of upper_bound. Is there a true lower bound selection function in stl that agrees with the normal definition of lower bound.
EDIT: It was too many negations in the docs which made me confused. The problem was that I got the same iterator. I solved it by subtracting 1 from lower_bound return value. I use it for interpolation:
float operator()(double f)
{
SpectrumPoint* l=std::lower_bound(beginGet(),endGet(),(SpectrumPoint){float(f),0.0f}
,SpectrumPoint::CompareFreqLessThan);
if(l>beginGet())
{--l;}
SpectrumPoint* u=std::lower_bound(beginGet(),endGet(),(SpectrumPoint){float(f),0.0f}
,SpectrumPoint::CompareFreqLessThan);
if(u==endGet())
{u=beginGet();}
if(l==u)
{
if(u==endGet())
{return u->amp;}
return l->amp;
}
double f_min=l->freq;
double A_min=l->amp;
double f_max=u->freq;
double A_max=u->amp;
double delta_f=f_max-f_min;
double delta_A=A_max-A_min;
return A_min + delta_A*(f-f_min)/delta_f;
}
I am sorry for this confusion :-(
Lower bound: first element that is greater-or-equal.
Upper bound: first element that is strictly greater.
Example:
+- lb(2) == ub(2) +- lb(6) +- lb(8)
| == begin() | == ub(6) | +- ub(8) == end()
V V V V
+---+---+---+---+---+---+---+---+---+---+---+
| 3 | 4 | 4 | 4 | 4 | 5 | 7 | 7 | 7 | 7 | 8 |
+---+---+---+---+---+---+---+---+---+---+---+
^ ^ ^
| | |
+- lb(4) +- ub(4) +- lb(9) == ub(9) == end()
|- eq-range(4) -|
As you can see, the half-open equal-range for n is [lb(n), ub(n)).
Note that both bounds give you meaningful insertion locations for an element of the desired value so that the ordering is maintained, but lower_bound has the distinguishing feature that if the element already exists, then you get an iterator which actually points to that element. Thus you can use lower_bound on an ordered range to implement your own unique-membership or multiple-membership container.
void insert(Container & c, T const & t)
{
auto it = std::lower_bound(c.begin(), c.end(), t);
// if unique container:
if (it != c.end() && *it == t) { /* error, element exists! */ return; }
c.insert(it, t);
}
It returns the iterator one past the last element that is less than the value asked for. This is useful as an insertion position (and that's why the function returns that iterator). It's also useful that the half-open range first, lower_bound(first, last, value) specifies all values less than value.
upper_bound returns the iterator one past the last element [less than or equal to / not greater than] the value asked for. Or strictly: the last element which the value is not less than, since both algorithms deal exclusively in less-than comparators.
If you want the iterator before the iterator returned by lower_bound, you can subtract 1 (for a random access iterator), decrement (for a bidirectional iterator), or do a linear search instead of using lower_bound (for a forward iterator that is none of those).
Beware the edge case that there is no element less than the value asked for, in which case you can't have what you want, because it doesn't exist. lower_bound of course returns the beginning of the range in that case, so doesn't need a special-case return value.
Since this has been reopened, I'll try to make my comment an answer.
The name lower_bound is mathematically incorrect. A better name might be least_upper_bound, but that would probably confuse most non-mathematically minded folk. (And then what do you call upper_bound? almost_least_upper_bound? Yech!)
My advice: Get over the fact that the names lower_bound and upper_bound are technically incorrect. The two functions as defined are quite useful. Think of those functions as a useful abuse of notation.
To make a mathematically correct lower_bound function that conforms with the C++ concept of an iterator, the function would have to return a reverse iterator rather than a forward iterator. Returning a reverse iterator is not nearly as useful as the approach taken by the perhaps misnamed lower_bound and upper_bound, and the concept of returning a reverse iterator runs afoul of the fact that not all containers are reversible.
Why a reverse iterator? Just as there is no guarantee that an upper bound exists in the container, there similarly is no guarantee that a lower bound will exist. The existing lower_bound and upper_bound return end() to indicate that the searched-for value is off-scale high. A true lower bound would need to return rend() to indicate that the searched-for value is off-scale low.
There is a way to implement a true lower bound in the form of a forward iterator, but it comes at the price of abusing the meaning of end() to mean "there is no lower bound". The problem with this abuse of notation is that some user of the function might do something equivalent to true_lower_bound(off_scale_low_search_value)-1 and voila! one has a pointer to the largest element in the set.
That said, here's how to do it. Have the true lower bound function return end() if the container is empty or if the searched-for value is smaller than the first value in the container. Otherwise return upper_bound()-1.
lower_bound, upper_bound and equal_range are functions which perform binary search in a sorted sequence. The need for three functions comes from the fact that elements may be repeated in the sequence:
1, 2, 3, 4, 4, 4, 5, 6, 7
In this case, when searching for the value 4, lower_bound will return an iterator pointing to the first of the three elements with value 4, upper_bound will return an iterator pointing to the element with value 5, and equal_range will return a pair containing these two iterators.
Following David Hammen's answer, I attempted to explain why we often don't feel the names of lower_bound/upper_bound to be correct, or at least intuitive.
It's because we are looking for an element immediately lower than the query.
I made a drawing and a use case:
Code:
template< typename T, typename U >
auto infimum(std::map<T,U> const& ctr, T query)
{
auto it = ctr.upper_bound(query);
return it == ctr.begin() ? ctr.cend() : --it;
}
template< typename T, typename U >
bool is_in_interval(std::map<T,U> const& ctr, T query)
{
auto inf = infimum(ctr, query);
return inf == ctr.end() ? false : query <= inf->second;
}
https://ideone.com/jM8pt3
Basically to get the behavior of the "grey arrows", we need upper_bound - 1 which is why it's weird.
Let me rephrase that slightly: from the name lower_bound we instinctively expect returns-first-immediately-inferior-element (like the grey arrows). But we get returns-first-immediately-superior-element for lower_bound; and first-immediately-strictly-superior-element for upper_bound. That's what is surprising.
It's surprising in the hypothesis that you work with a sparse sequence like my thought experiment in the picture above. But it makes wonderful sense when you think of it in terms of «bounds of an equal_range» in a dense sequence, populated with plateaus, like Kerrek SB beautifully pictured.
Test code:
#include <map>
#include <cassert>
#include <iostream>
// .. paste infimum and is_in_interval here
int main()
{
using std::cout;
using Map = std::map<int,int>;
Map intervals{{2,5}, {8,9}};
auto red = infimum(intervals, 4);
assert(red->first == 2);
cout << "red->first " << red->first << "\n";
auto green = infimum(intervals, 6);
assert(green->first == 2);
cout << "green->first " << green->first << "\n";
auto pink = infimum(intervals, 8);
assert(pink->first == 8);
cout << "pink->first " << pink->first << "\n";
auto yellow = infimum(intervals, 1);
assert(yellow == intervals.cend());
auto larger_than_all = infimum(intervals, 15);
assert(larger_than_all->first == 8);
bool red_is = is_in_interval(intervals, 4);
cout << "red is in " << red_is << "\n";
bool green_is = is_in_interval(intervals, 6);
cout << "green is in " << green_is << "\n";
bool pink_is = is_in_interval(intervals, 8);
cout << "pink is in " << pink_is << "\n";
bool yellow_is = is_in_interval(intervals, 1);
cout << "yellow is in " << yellow_is << "\n";
}
results in
red->first 2
green->first 2
pink->first 8
red is in 1
green is in 0
pink is in 1
yellow is in 0
seems ok.
So of course the utility functions are not very good, they should be designed with a range API, so we can work with any collection or sub-range, or reverse iterators, or filtered views and whatnot. We can get that when we have C++20. In the meantime, I just made a simple educative map-taking API.
play with it:
https://ideone.com/jM8pt3
Another usage of lower_bound and upper_bound is to find a range of equal elements in a container, e.g.
std::vector<int> data = { 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6 };
auto lower = std::lower_bound(data.begin(), data.end(), 4);
auto upper = std::upper_bound(lower, data.end(), 4);
std::copy(lower, upper, std::ostream_iterator<int>(std::cout, " "));
Auch!
Did you change the original code or is the copy-paste error in there since day one?
float operator()(double f)
{
SpectrumPoint* l=std::lower_bound//...
...
SpectrumPoint* u=std::lower_bound//...
...
}
In the code I read today you are assigning lower_bound to both 'l' and 'u'.

Fixed length structure re-arrange: should I use array or linked list?

This structure will only contain exactly 256 unsigned chars.
The only operation possible is taking random characters from it and putting them in front of the structure.
For example:
A= 'abcdef'
Move character 2 to front
A= 'cabdef'
I first thought of using linked link so that it could work as a queue.
Thing is I heard that arrays are much faster than linked lists for these operations. Is this true?
A linked list will be O(1) and an array will be O(n) for a move operation as you have described. For small n the array will probably be faster, but the only way to know for sure is to benchmark.
In a case like this I would code what's clearest and only worry about efficiency if it proves to be a bottleneck.
P.S. I made an assumption that you already had a pointer to the character you want to move. If this is not the case, then finding the character will be O(n) in the linked list and you will lose any advantages it might have.
Use an array. The linked list will be huge and unwieldy for storage of char data.
A linked list would be a good approach since you don't need to move all the intermediate elements around. std::list works just fine, combined with splice(). You will need an iterator to the element you want to move to the front:
#include <list>
#include <iostream>
#include "prettyprint.hpp"
int main()
{
std::list<int> x { 1, 4, 6, 7, 2 };
auto i = x.begin(); std::advance(i, 2); // i points to 6
std::cout << x << std::endl; // [1, 4, 6, 7, 2]
x.splice(x.begin(), x, i);
std::cout << x << std::endl; // [6, 1, 4, 7, 2]
}
(Using the pretty printer for a quick demo.)
As others have said, whether that's more efficient that a random-access container depends on how you are tracking the element that you want to move.
Update: In light of Steve's remarks I should like to offer a raw C-array solution, too. It has the benefit that you can access it by position in O(1) time and that it requires minimum space:
char y[] = { 'a', 'c', 'Q', '%', '5' };
std::cout << pretty_print_array(y) << std::endl; // [a, c, Q, %, 5]
std::rotate(y, y + 2, y + sizeof(y));
std::cout << pretty_print_array(y) << std::endl; // [Q, %, 5, a, c]
The rotate call could be wrapped in a function:
template <typename T, size_t N>
void bring_forward(T (& a)[N], size_t p) { std::rotate(a, a + p, a + N); }
In C++, you can use a vector instead of array or linked list. The complexity of a Linked List is O(1) like #Mark Ransom said. With the vector, you can use the command rotate to perform the action you desire. The complexity is determined by the number of swaps.
From MSDN, how to use rotate:
const int VECTOR_SIZE = 8;
// Define a template class vector of strings
typedef vector<string> StrVector;
//Define an iterator for template class vector of strings
typedef StrVector::iterator StrVectorIt;
StrVector Tongue_Twister(VECTOR_SIZE);
StrVectorIt start, end, middle, it;
// location of first element of Tongue_Twister
start = Tongue_Twister.begin();
// one past the location last element of Tongue_Twister
end = Tongue_Twister.end();
// Initialize vector Tongue_Twister
Tongue_Twister[0] = "she";
Tongue_Twister[1] = "sells";
Tongue_Twister[2] = "sea";
Tongue_Twister[3] = "shells";
Tongue_Twister[4] = "by";
Tongue_Twister[5] = "the";
Tongue_Twister[6] = "sea";
Tongue_Twister[7] = "shore";
middle = start + 3; // start position for rotating elements
cout << "Before calling rotate" << endl;
// print content of Tongue_Twister
cout << "Try this Tongue Twister:";
for (it = start; it != end; it++)
cout << " " << *it;
// rotate the items in the vector Tongue_Twister by 3 positions
rotate(start, middle, end);
Or you can do it with arrays:
// create an array of 9 elements and rotate the entire array.
int myArray[SIZE] = { 7, 8, 9, 1, 2, 3, 4, 5, 6, };
std::rotate(myArray, myArray + 3, myArray + SIZE);
How fast is this? I don't know. I haven't benchmarked it. But it is much easier than having to manually swap elements of an array.
The array will be O(1) to find the item and O(n) to move it and the other items into the correct position. The linked list will be O(n) to find the item and O(1) to move everything to the right position. The complexity is the same either way.
They're both easy to implement, so implement both of them and run tests to see which one lets your program run faster with real-world data.
C++ arrays are linked lists, so moving an element to the front of your list is cheap, provided you already know where the element is (i.e. you have an iterator on it). Using a vector would cause the entire list to be shifted each time an element is pushed in front of it.