I try to write a program check a string is Palindrone or not using vector and iterator.
I create definition in iterator.h file:-
#pragma once
#ifndef iterator
#define iterator
template<typename Bidrectional>
bool isPalindrone(Bidrectional first, Bidrectional end);
template<typename Bidrectional>
inline bool isPalindrone(Bidrectional first, Bidrectional last)
{
while (true)
{
last--;
if (first == last)
break;
if (*first != *last)
return false;
first++;
if (first == last)
{
break;
}
return true;
}
}
#endif
Now I got so many error in compile time:-
Severity Code Description Project File Line Source Suppression State
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 1640 Build
Error C2976 'std::reverse_iterator': too few template arguments c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 656 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 906 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 939 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 943 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 947 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 1170 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 1186 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 1563 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 1583 Build
Error C2059 syntax error: '=' c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 654 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 1650 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 2039 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.32.31326includevector 2035 Build
In main function I call is
#include <iostream>
#include "iterator.h"
#include <vector>
using namespace std;
int main()
{
vector<string> s1;
s1.push_back("Otto");
isPalindrone(s1.begin(), s1.end());
}
I am new to STL, Could you please help in this regards.
3
Answers
Little modification @oraqlle solution:-
Initial Problem
You’ve
#define iterator
meaning that everywhere that the symboliterator
is used it is replaced with, well nothing but this essentially deleted the symbol iterator everywhere it’s used meaning that vector and string can’t find their iterator types. Try using a different name for the header guard and all capital letters is ideal by convention for macros. Also try giving the header a more complex name as iterator is too generic and could cause future errors.isPalindrone.h
would be better.Eg.
Main Question
As for the main part of you’re code, using a
std::vector<std::string>>
. This means that the iterator returned froms1.begin()
ands1.end()
are iterators tostd::string
types, not to the elements of the string, ie. the characters of the string. This means that, for examples1.begin()
actually returns an iterator to astd::string
with the value"Otto"
not to a character'O'
like I assume you want.std::vector
is C++’s dynamic array type as opposed tostd::string
which (as the name suggests) is the most basic string type in C++.You have two options to fix this. First
std::string
has iterators of its own which you can pass to you’reisPalindrone()
function as it stands, or you can create astd::vector<char>
and push back each character. This vectors iterators will point to the elements in the vector, ie. iterators tochars
. I would personally use astd::string
as its easier to manage string types this way but both options work if you were to expand beyond words to say numbers maybe.Using
std::vector<char>
Here’s a quick way to make a string into an array of characters.
Alternative
I know you might be trying to learn iterators and algorithm patterns in C++ but if you want an alternative to approach to achieve the same task, is you can use the
std::equal
algorithm (found in the<algorithm>
header) andstd::string
‘s reverse iterators (the.rbegin()
and.rend()
methods).Quick Fix for
isPalindrone()
One thing you forget to do in you’re implementation of
isPalindrone()
is you don’t decrement theend
iterator each iteration. By decrementingend
each loop you move backwards through the letters of the word, comparing each letter from the front forwards to the back backwards. It also should be noted that anything using templates in C++ must be in a header so the functionisPalindrone()
must all be in a header.Updated Code
Links and Resources
I mentioned a lot so here are links to everything I used to answer the question.
std::string
links tostd::basic_string
because it is used to implement all standard string types in C++. Scroll down a bit for details.If there is anything I missed; edits and constructive comments to help improve the answer would be appreciated.
The problem is this line from the header file:
From that point onward, all mentions of the symbol
iterator
will be that macro. And it will expand to nothing.So when e.g.
std::vector
defines itsiterator
type, it can’t.For header include guards use more complex names. Even using all upper-case (which is what most people uses for macros) would be enough.
Since your header file name is
iterator.h
(which is really a bad and misleading name) I suggest something likeITERATOR_H
instead.