comparing against last element of vector

Oct 9, 2013 at 4:41am


1
2
3
4
5
6
7
8
9
int main(){
    std::vector<std::string> v = {"test", "tester", "tester"};
        
    for (auto i:v){
        if (i == v.back()){
            std::cout << "last element" << std::endl;
        }
    }
}


I am trying to get the if condition to execute only on the last element. I am not sure on how to do this?
Last edited on Oct 9, 2013 at 5:18am
Oct 9, 2013 at 5:25am
You could make something prettier than this with iterators.
1
2
3
4
5
for(auto i:v) {
	if(i==v.size()-1) {
		//blah
	}
}
Oct 9, 2013 at 5:28am
Oct 9, 2013 at 5:31am
Use iterators, or distance function from algorithm library.
Last edited on Oct 9, 2013 at 5:40am
Oct 9, 2013 at 5:34am
at this point i am not trying ot string out the vector elements, i am just interested in checking a vector for the last element, even if other elements contain the same value.

@xismn
that would be comparing a string against an int as size() returns the integer size of the vector. Iterators also bring back the same results of executing from both the second and last element of the vector example, instead of only the last element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){
    std::vector<std::string> v = {"test", "tester", "tester"};
    std::string *last = &v.back();
        
    for (auto i:v){
        if (i == v.back())
            std::cout << "last element" << std::endl;
        if (&i == last)
            std::cout << "yes" << std::endl;
    }
    
    for (std::vector<std::string>::iterator it = v.begin(); it < v.end(); ++it){
        if (*it == v.back())
            std::cout << "back" << std::endl;
    }
}

1
2
3
4
last element
last element
back
back
Last edited on Oct 9, 2013 at 5:36am
Oct 9, 2013 at 7:37am
i just now figured out i guess what you guys meant.
1
2
3
4
5
6
7
8
9
10
11
int main(){
    std::vector<std::string> v = {"test", "tester", "tester"};
    
    for (std::vector<std::string>::iterator it = v.begin(); it < v.end(); ++it){
        if (&v.back() == &*it){ //if address of index == address of iterator
            std::cout << &v.back() << " == " << &*it;
        }
    }


}


I switched out this method in the function overload i was intially usng, and now i get an error i am not sure what it means:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::ostream& operator<<(std::ostream& stdout, const File& obj){
    std::string start = "[", end = "]", delim = ", ";
    std::string s;
    if (obj.tokens.size() > 0){
        s += start;
        //for (auto i: obj.tokens){
        for(std::vector<std::string>::iterator it = obj.tokens.begin(); it != obj.tokens.end(); ++it){
            s += *it;
           // if (i != obj.tokens.back()){
            //    s += delim;
            //}
            if (&obj.tokens.back() == &*it){ //if address of index == address of iterator
                s += delim;
            }
        }
        s += end;
    }
    else{
        s += start;
        s += end;
    }
    stdout << s;
    return stdout;
}

1
2
3
4
files.cpp: In function ‘std::ostream& operator<<(std::ostream&, const File&)’:
files.cpp:46:70: error: conversion from ‘std::vector<std::basic_string<char> >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ to non-scalar type ‘std::vector<std::basic_string<char> >::iterator {aka __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ requested
         for(std::vector<std::string>::iterator it = obj.tokens.begin(); it != obj.tokens.end(); ++it){
                        
Last edited on Oct 9, 2013 at 7:47am
Oct 9, 2013 at 7:56am
It looks like you did not follow the link.
1
2
for (const auto& i : v) {
  if (&i != &v[0]) {


Anyway, your error message is clear. obj is const. obj.tokens is const. Iterators to obj.tokens must be const_iterator. You want a non-const iterator, which thus is an error. You don't need a non-const iterator ...
Last edited on Oct 9, 2013 at 7:57am
Oct 9, 2013 at 7:59am
the error tells you what's wrong:
1
2
3
for(std::vector<std::string>::const_iterator it = obj.tokens.begin(); it != obj.tokens.end(); ++it){ // since obj is const you need a const iterator
...
}
Oct 9, 2013 at 8:07am
ah.. yeah, that did not click with me

thanks
Oct 9, 2013 at 8:18am
An alternative:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <vector>

int main()
{
    typedef std::vector<std::string> vec_type;
    vec_type v = { "test", "tester", "tester" };

    vec_type::iterator it = v.begin();
    while (it != v.end())
    {
        const std::string& s = *it;

        if (++it == v.end())
            std::cout << "LAST: " << s << '\n';
        else
            std::cout << s << '\n';
    }
}


http://ideone.com/lJbhii
Topic archived. No new replies allowed.