#Why auto instead of int? (Crbegin, crend)
78 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
@vast hornet
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
crbegin does not result in an int
it results in an iterator
auto is frequently used for iterator variables
specifically std::string::const_reverse_iterator
Okay, I just saw that. one more moment, question on this is going to pop up
how can I state reverse iterator, or am I not allowed to do that?
becasue I have iterator within the include tags now.
!cppref std::reverse_iterator
template<class Iter>
class reverse_iterator;
Ohh okay, I thought it was going to want me to use const_reverse_iterator
if you do std::string::const_reverse_iterator i=... that will work
instead of just a normal one.
btw putting a semicolon after main isn't necessary, and same for the parentheses with return
Okay, also why can this work with normal reverse iterator and const reverse iterator? I'm gussing inheratance?
wdym
for the type of i?
yes
;compile cpp std::string s; std::string::reverse_iterator i=s.crbegin();
In file included from /opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/cassert:43,
from /opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/x86_64-linux-gnu/bits/stdc++.h:33,
from <source>:1:
/opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/bits/stl_iterator.h: In instantiation of 'constexpr std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&) [with _Iter = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; _Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >]':
<source>:5:43: required from here
5 | std::string::reverse_iterator i=s.crbegin();
| ^
/opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/bits/stl_iterator.h:216:9: error: no matching function for call to '__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >::__normal_iterator(const __gnu_cxx::__normal_iterator<const ch
doesn't seem to work
okay, my bad on that. but are you able to explain why it's allowed?
or should I just avoid doing something out of the library intentions in general
std::reverse_iterator i=test.crbegin() is creating an object of type std::string::const_reverse_iterator
it's deducing the template arguments for the template std::reverse_iterator
and the result of test.crbegin() is std::reverse_iterator<std::string::const_iterator>
so it sees that the input is a reverse iterator already, and it just uses the same type
i see that its already been explained
I thought that would only be a const_reverse_iterator also
if you try to assign to *i you should see that it is a const iterator
general rule of thumb is to use "auto" when doing range loops, or when you're trying to use an iterator as it makes code readable.
You can technically write out the entire type that you're using, but it's not friendly to see.
An int would be used if you're iterating over integers. But a reverse iterator is not at all similar to a int. Two completely different types/objects
so if you wanted to do
and that's due to the template, so it's justusing that const_iterator template if I am setting it to i
Also, yeah I see what you mean
yes
!f
!f
int main() {
std::string test = "Hello";
for (int i = test.size() - 1; i >= 0; --i) {
std::cout << i << " ";
}
return 0;
}
this is essentially the same thing as what you orignially intended
but with the use of ints
should use std::size_t not int for the loop preferably
okay, but that was just for the I situation. aka the template metaprogramming because I shoved in just a normal reverse iter
since that is what test.size() results in
Looking
that's true, but i wanted to show him an example of it in an int, even if i broke some standards doing so lol
Std::size_type right?
i started with cplusplus, but cppref is much better once you get used to it
another reason why "auto" is preferable in this case. Prevents user error from happening; the compiler knows what the type should be
@velvet stirrup on cpp refrence where is the return value? as in the outright stated return?
oeither one of you
I must be looking at the wrong thing one sec
okay, yeah I found the right one. thanks foor all the help guys!
also @shrewd goblet yeah
and why is it being called basic string?
because std::string is just a specific specialization of std::basic_string