Properly written for-loops, normal and inverse, C ++ basics

For loops are trivial, right? Well, I wondered about some things, probably because I'm a physicist and I don't have proper programming. Let's use an example of having a string of words, and we want to print seperatelly characters, and then in reverse order.

Q1) Should we declare a variable for size, assign it and use it, or call the size () function of the string, inside the for loop

string s = "asdf";
int size = s.size();

for (int i = 0; i<size; i++){
 cout<<s[i]<<endl;
}

//or

for (int i = 0; i<s.size(); i++){
 cout<<s[i]<<endl;
}

// Q2) If we want to print backward characters, what is more correct? Also, am I using the new variable size again?

//AND)

for (int i = size-1; i>= 0; i--){
 cout<<s[i]<<endl;
}

//AT)

for (int i = size; i>= 0; i--){
 cout<<s[i - 1]<<endl;
}

//WITH)

for (int i = size - 1; i>= 0; i--){
 cout<<s[i]<<endl;

}

// D) // I used this in case I had to use I for another function, where it should be increased. (ok sure a.size = s.size)

for (int i = 0; i<size; i++){
 cout<<s[size - i - 1]<<endl;
// cout<<a[i]<<endl;
}

Q3) Which resource is most convenient?

Q4 ). ( ).

Q5) ?

.

+4
6

, : -):

for ( auto current = s.cbegin(), end = s.cend(); current != end; ++ current ) {
    std::cout << *current << std::endl;
}

crbegin() crend() cbegin() cend(). ( ++ 11. , std::string::const_iterator c .)

(, ), :

for ( int i = 0; i != s.size(); ++ i )...

:

int i = s.size();
while ( i != 0 ) {
    -- i;
    //  ...
}

(, , , , . , s.size() .)

+4

: . , . , i < sth.size() , .

-: 10 ^ 6 ? .

-: : , . , , :

for (int i = static_cast<int>(str.size()) - 1; i >= 0; i--)
    std::cout << str[i] << endl;

: . (, , ).

std::for_each(test.rbegin(), test.rend(), [=](char c) { std::cout << c << std::endl; });
+2

-, , , . GCC, g++ -Wall -Wextra -g. , .

, ( , , , , , ). , , , .

Q1) , ?

,

string s = "asdf";
int size = s.size();
for (int i = 0; i<size; i++){
   cout<<s[i]<<endl;
}

for (int i = 0; i<s.size(); i++){
   cout<<s[i]<<endl;
}

, ( size ) , , .

(, g++ -O2 GCC) , , ( ) , , for (int i=0; i<4; i++) ( )

, C++11 (, GCC 4.8 4.9 g++ -std=c++11), , a :

for (char c : s) 
   cout << c;

++ 11 c for (auto c: s)

+1

, , . .

,

string s = "asdf";
int size = s.size();

for (int i = 0; i<size; i++){
 cout<<s[i]<<endl;
}

string s = "asdf";
string::size_type n = s.size();

for ( string::size_type i = 0; i < n; i++ ){
 cout << s[i] < <endl;
}

- , size() . , . , ,

string s = "asdf";
string::size_type n = s.size();

for ( string::size_type i = 0; i < n; i++ ){
 cout << s[i] < <endl;
}

,

string s = "asdf";

for ( string::size_type i = 0, n = s,size(); i < n; i++ ){
 cout << s[i] < <endl;
}

for ( string::size_type i = 0; i < s.size(); i++ ){
 cout << s[i] < <endl;
}

, , s.size(). , .

for ( string::size_type i = s.size(); i != 0; i-- ){
 cout << s[i - 1] << endl;
}

for ( string::size_type i = s.size(); i != 0;  ){
 cout << s[--i] << endl;
}

,

for (int i = size - 1; i>= 0; i--){

, == 0 , , , != 0. , , int std::string:: size_type.

+1

Spook, :)

++ 11 - Q1:

std::string myString("foo");
for (char c : myString)
{
    // TODO:
}

, , , , std::reverse(myString.begin(), myString.end());, .

0

:

for(size_t i = 0, count = s.size(); i < count; i++) {...}
for(size_t i = s.size(); i--;) {...}

?
, , .

, std::vector<> .. , for(size_t i = 0; i < s.size(); i++) (, , ). ( inline), , . , .

In addition, I try to keep my code concise. Especially the reverse option is as small as it can get. Note that it correctly iterates from i = s.size() - 1to i = 0, evaluating the condition to the first iteration, which will reduce ito the first valid value to be used.


The reverse iteration should not be done as follows:

for(size_t i = s.size(); i > 0; i--) {
    cout<<s[i - 1]<<endl;
}

This is error prone because it requires the programmer / maintainer to remember that the index is the only one on the whole body of the loop.

0
source

Source: https://habr.com/ru/post/1544141/


All Articles