vector with function

Write a function palindrome that takes a vector parameter and returns true
or false according to whether the vector does or does not read the same
forward as backward (e.g., a vector containing 1, 2, 3, 2, 1 is a palindrome, but
a vector containing 1, 2, 3, 4 is not).


[code]
#include <vector>
#include <iostream>
using namespace std;
void palindrome(vector<int>);
int main()
{
int x;
vector<int>values;
while(!x ==".")
{
cout<<"enter your numbers (end with a \".\"):";
cin>>x;
}

palindrome(values);
return 0;
}

void palindrome(vector<int>vect)
{
int f;
f=vect.reverse();
if(f/x==1)
cout<<"palindrome"<<endl;
else cout<<"not palindrome"<<endl;

}
[code]




can anyone help me to sort whats wrong.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <vector>
#include <iostream>
#include <algorithm>

bool palindrome( const std::vector<int>& );

int main()
{
    int x;
    std::vector<int> values ;
    
    std::cout << "enter your numbers (end with a non-digit): ";
    
    // http://en.cppreference.com/w/cpp/container/vector/push_back
    while( std::cin >> x ) values.push_back(x) ;  
    
    for( int v : values ) std::cout << v << ' ' ;

    if( palindrome(values) ) std::cout << " is a palindrome.\n" ;
    else std::cout << "is not palindromic.\n" ;
}

bool palindrome( const std::vector<int>& vect )
{
    // http://en.cppreference.com/w/cpp/algorithm/equal
    // probably, the assignment expects you to write your own equivalent code
    // instead of using the standard algorithm.
    return std::equal( vect.begin(), vect.begin() + vect.size()/2, vect.rbegin() ) ;
}

http://coliru.stacked-crooked.com/a/e8dd7825f7ffe492
thks for the reference.. very useful...

can u xplain to me.. the line 17....its nt clear?

+ line 17 bugs says erreur
Last edited on
yea for this part we were not really expected to use algorithm...
and its a bit complicated for me to understand the way u did it..

can u modify on mine. or tell me what are my erreur?
> + line 17 bugs says erreur

Line 17 is a range-based for loop (added in the 2011 revision of the standard).
for( int v : values ) reads as: for each int v in the sequence values
See: http://www.stroustrup.com/C++11FAQ.html#for


> its a bit complicated for me to understand the way u did it..

Check if the left half of the sequence is (lexicographically) equal to the right half in reverse.
Sequence: 1 2 3 4 5 4 3 2 1
size: 9, size/2 == 4
From begin() up to, not including begin()+size/2: 1 2 3 4
From rbegin(): starting from the last element (the last 1) in reverse.


Topic archived. No new replies allowed.