vector <int>v;
vector <int>:: iterator it,it1;
it=v.begin();
it1=v.end();
v.insert(it,12); // in this line doesn't matter what i write it or it1
it=v.begin(); it1=v.end(); // here *it=12, also it is first element and it1 = last element + 1
v.insert(it1,15); // if i write it instead of it1, then v contains 15 12
it=v.begin(); it1=v.end();
cout<<*(it+1); // this line outputs 15;
if my comments are correct, then how v.insert(v.end(), any number); can be correct?
Never debug comments. Always debug the code. :)
std::vector::insert(iterator, value) inserts the value before the iterator passed in. Since std::vector::end() is passed to insert(), this inserts 15 before the end of the vector - that is, after 12. Therefore when the element after the first element is printed, this should print 15.
iterator insert( iterator pos, const T& value ); inserts the element before the specified position.
So here it will insert at the back, and then the end position will shift by one.
Your it1=v.end() will be invalidated by the insert, so you'll need to re-set it. (Thanks #Mgetz)
To insert into an empty vector, you can still insert to end. Just because end() == begin() in an empty vector doesn't mean they're set to the same thing.
Related
Writing a function that takes a generic for loop iterator consisting of the iterator function, the invariant state & the loop control variable to return the value the control variable has in the last iteration is straightforward:
function iterator_last_value(iterator, state, control_var)
local last
for value in iterator, state, control_var do
last = value
end
return last
end
print(iterator_last_value(("hello world"):gmatch"%a+")) -- world
This could be easily extended to support arbitrary constant numbers of arguments up to Lua's local register limit. We can also add vararg iterator return value support by always storing the last vararg in a table; this requires us to get rid of Lua's for loop syntactic sugar:
function iterator_last(iterator, state, control_var)
local last = {}
local last_n = 0
local function iter(...)
local control_var = ...
if control_var == nil then
return table.unpack(last, 1, last_n)
end
last = {...}
last_n = select("#", ...)
return iter(iterator(state, control_var))
end
return iter(iterator(state, control_var))
end
print(iterator_last(ipairs{"a", "b", "c"})) -- 3, c
which works well but creates a garbage table every iteration. If we replace
last = {...}
last_n = select("#", ...)
with
last_n = select("#", ...)
for i = 1, last_n do
last[i] = select(i, ...)
end
we can get away with reusing one table - presumably at the cost of manually filling the table using select being less efficient than {...}, but creating significantly fewer garbage tables (only one garbage table per call to iterator_last).
Is it possible to implement a variadic return value iterator_last without storing a vararg with significant overhead using a table, coroutine or the like, leaving it on the stack and only passing the varargs around through function calls? I conjure that this is not possible, but have been unable to prove or disprove it.
openedge 12.2 win64 tty
I have a string of comma separated items that the user fills in manually. Elements have a certain structure, and the chk_store method checks each element for correctness of filling and returns the position of the beginning of the first incorrectly filled element. I want that at the on leave event, if there is an incorrectly filled element in the line, then the cursor will move to the position of the beginning of this element. The problem is that the cursor-offset is not set to the position I need when executing the code below.
ON LEAVE OF cfrm-store IN FRAME fmnu
DO:
DO ON ERROR UNDO, RETURN NO-APPLY:
ASSIGN cfrm-store.
icfs = chk_store(cfrm-store).
IF icfs = 0 THEN
LEAVE.
SELF:CURSOR-OFFSET = icfs.
RETURN NO-APPLY.
END.
END.
edit 1.
on leave doesn't work in the console, at least with this syntax, but it works fine in prowin.
def var c as c no-undo init '1234567890' FORMAT 'x(20)'
.
DEF VAR d AS c NO-UNDO INIT '1234567890' FORMAT 'x(20)' .
def button bok auto-go.
def frame f
skip(1)
c
SKIP(1)
d
skip(1)
bok
with view-as dialog-box side-labels.
on leave of c,d in frame f
do:
assign c d.
IF length(SELF:SCREEN-VALUE) <= 10 THEN
DO:
SELF:CURSOR-OFFSET = length(SELF:SCREEN-VALUE) + 1.
RETURN NO-APPLY.
END.
end.
display c d with frame f.
enable all with frame f.
wait-for go of frame f.
I assume that your chk_store function returns the position of the first incorrect element in the list. But to set the cursor position, you need the position of the first incorrect element in the string.
See the example below :
DEFINE VARIABLE list AS CHARACTER NO-UNDO.
DEFINE VARIABLE icfs AS INTEGER NO-UNDO.
DEFINE VARIABLE pos AS INTEGER NO-UNDO.
list = "ok,ok,ok,ok,nok,ok,ok".
icfs = LOOKUP("nok",list). /* = 5 : the position in the list */
pos = INDEX(list, ENTRY(icfs,list)). /* = 13 : the position in the string */
I am trying to erase the element that I just traversed into. I initially forgot to store the return value of s1.erase(... inside the for loop to ultimately set the condition to exit the loop. With the way the code is right now, I expected the loop to continue indefinitely. But it works just the way it was originally intended to work.
It looks like std::erase advances the iterator and stores the value in rit. I am not able to find any documentation that explains this behavior.
https://en.cppreference.com/w/cpp/container/set/erase says that the iterator returned has to be stored. All arguments of the set::erase are passed by value, so how is the reverse iterator being advanced?
How is this loop completing?
std::set<int> s1;
s1.insert(20);
s1.insert(30);
s1.insert(50);
auto rit = s1.rbegin();
for (; rit!= s1.rend();)
{
std::cout << "rit is " << *rit << " size is " << s1.size() << std::endl;
s1.erase(std::next(rit).base());
std::cout << "rit after erase is " << *rit << std::endl;
}
The output is
rit is 50 size is 3
rit after erase is 30
rit is 30 size is 2
rit after erase is 20
rit is 20 size is 1
Segmentation fault
Recall that reverse_iterator::base() is always one element behind the apparent iterator's value. For example, after auto rit = s1.rbegin(), *rit returns the last element, while rit.base() == s1.end().
In other words, *rit == *prev(rit.base())
In your loop, initially rit.base() == s1.end(). Then std::next(rit).base() refers to the last element; that element is being erased. In std::set, erasing an element only invalidates iterators to that element, but not any others. s1.end() is still a valid iterator, and so is rit, with rit.base() still equal to s1.end(). So on the next iteration of the loop, you erase the last element again, and leave rit.base() == s1.end() again. And so on.
At some point, the last element is erased, s1 becomes empty, and then *rit exhibits undefined behavior. Recall that *rit == *prev(rit.base()), but there is no previous element anymore.
I'm trying to print to the file amount of microseconds:
high_resolution_clock::time_point t1 = high_resolution_clock::now();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration1 = duration_cast<microseconds> (t2-t1).count();
fprintf(file, "%lu, %lu\n", dutation1, duration1);
In the file I can see the first column having some values around 2000 but
I get second column values always equal to zero. I wonder if I'm doing correct fprintf (the %lu parameter) and why does it print the second variable as zero in the file?
The count function returns a type called rep, which according to this std::duration reference is
an arithmetic type representing the number of ticks
Since you don't know the exact type, you can't really use any printf function to print the values, since if you use the wrong format you will have undefined behavior (which is very likely what you have here).
This will be easily solved if you use C++ streams instead, since the correct "output" operator << will automatically be selected to handle the type.
I am doing a problem and i need to do this task.
I want to add pairs (p1,q1),(p2,q2)..(pn,qn) in such way that
(i) Duplicate pair added only once(like in set).
(ii) I store count how many time each pair are added to set.For ex : (7,2) pair
will present in set only once but if i add 3 times count will 3.
Which container is efficient for this problem in c++?
Little example will be great!
Please ask if you cant understand my problem and sorry for bad English.
How about a std::map<Key, Value> to map your pairs (Key) to their count and as you insert, increment a counter (Value).
using pairs_to_count = std::map<std::pair<T1, T2>, size_t>;
std::pair<T1, T2> p1 = // some value;
std::pair<T1, T2> p2 = // some other value;
pairs_to_count[p1]++;
pairs_to_count[p1]++;
pairs_to_count[p2]++;
pairs_to_count[p2]++;
pairs_to_count[p2]++;
In this code, the operator[] will automatically add a key in the map if it does not exist yet. At that moment, it will initialize the key's corresponding value to zero. But as you insert, even the first time, that value is incremented.
Already after the first insertion, the count of 1 correctly reflects the number of insertion. That value gets incremented as you insert more.
Later, retrieving the count is a matter of calling operator[] again to get value associated with a given key.
size_t const p2_count = pairs_to_count[p2]; // equals 3