Форум » C/C++ для начинающих (C/C++ for beginners) » Basic Palindrome vector » Ответить

Basic Palindrome vector

Felicia123: [pre2]#include <iostream> #include <vector> #include <string> using namespace std; int main(){ string words; cout <<"Enter words : " ; getline( cin,words ); string wordsReverse( words.rbegin() , words.rend() ); cout << "\nBackwards , it's : " << wordsReverse << endl << endl; if( words == wordsReverse ) cout << words << " is a palindrome !" << endl; else cout << words << " is not a palindrome ! " << endl; system( "pause" );//Pause window return 0;//Exit Program }[/pre2] i know it's not hard . but the problem is , here is the question A palindrome is a sequence of characters, numbers or words that have the property of reading the same in either direction (backwards or forwards). In a palindrome white space and punctuation have no significance (they are typically ignored). Examples of palindromes are shown below. 121 - A numeric sequence GACTTCAG - A DNA Sequence Sator Arepo tenet opera rotas - A latin phrase for: The farmer by his labour keeps the wheels to the plough. As you can see in the last example, white space and capitalisation are ignored. You are to write a program that takes in a sequence and works out if it is/isn’t a palindrome. We are going to do this in three different ways. how to ignore capitalisation and while space ? Besides that . how to change the iterator STL to vector STL ?

Ответов - 39, стр: 1 2 All

Сыроежка: I have not understood the last question about changing the STL iterator to the STL vector. What did you mean? To ignore white spaces and other non-alpha-digit symbols and to ignore capitalization you can for example copy a string into a vector of characters and then compare the vector with it itself using reverse iterators. Or you can write an appropriate function yourself. For example (without testing) [pre2] for ( char c : words ) { if ( isalnum( c ) ) v.push_back( toupper( c ) ); } if ( v == std::vector<char>( v.rbegin(), v.rend() ) ) std::cout << "It is a polindrome\n";[/pre2]

Felicia123: Sorry sir . here i did it [pre2]// Palindrome.h // start points to the beginning of the data and end points to one past the end template<typename BidirectionalIterator> bool IsPalindrome(BidirectionalIterator start, BidirectionalIterator end) { if (start == end - 1) return true; end--; while (start <= end) { if (*start == *end) { start++; end--; } else return false; } return true; } //Main.cpp #include <iostream> #include <string> #include <vector> using namespace std; ostream & operator<<(ostream &, vector<int> &); int main() { string word = "tattarrattat"; // longest palindrome in English vector<int> number; for (int i = 0; i < 5; i++) number.push_back(i); for (int j = 4; j >= 0; j--) number.push_back(j); if (IsPalindrome( word.begin(), word.end())){ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is a palindrome.\n"; } else{ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is not a palindrome.\n"; } if (IsPalindrome(number.begin(), number.end())){ int Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is a palindrome." << endl; } else{ int Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is not a palindrome." << endl; } system( "pause" ); return 0; } ostream & operator<<(ostream & stream, vector<int> & vec) { for(int i = 0; i < vec.size(); i++) cout << vec; return stream; }[/pre2] my problem is . for the ReverseNumber variable , why I cnanot declare it as int Reversenumber ( number.rbegin() , number.rend() ); it got error error C2440: 'initializing' : cannot convert from 'std::reverse_iterator<_RanIt>' to 'int'

Сыроежка: Your code is invalid. Let consider template function IsPalindrome First of all start can be equal to end. So you may not use expression start == end - 1 It would be more correctly to write if ( start == end || start == std::prev( end ) ) return true; Moreover you wrote that start and end are BiidirectionalIterator. Such iterators have no overload operators as end -1 or start <= end. These operators can be used only for random access iterators. As for the error then an object of type int shall be initialized by an integral expression.


Felicia123: shall be initialized by an integral expression. ? can provide some sample for me?

Сыроежка: int Reversenumber( 10 ); or int Reversenumber = 10; or int Reversenumber = *number.begin(); Here 10 and *number.begin() are integral expressions.

Felicia123: i get it . by the way , this is point to the begin only right? so if i wan point to end also? since i wan print from backward to the forward?

Сыроежка: An object of type int does not points anywhere. It stores an integral value.:) I think that you meant a vector instead of an object of type int. So you should write std::vector<int> Reversenumber ( number.rbegin() , number.rend() );

Felicia123: it's work ! here the full code implement [pre2]// start points to the beginning of the data and end points to one past the end #include <iostream> #include <string> #include <vector> using namespace std; template<class T> bool IsPalindrome(T start, T end){ if ( start == end || start == std::prev( end ) ) return true; end--; while (start <= end){ if (*start == *end){ start++; end--; } else return false; } return true; } ostream & operator<<(ostream &, vector<int> &); int main() { string word = "GACTTCAG"; // longest palindrome in English vector<int> number; for (int i = 0; i < 5; i++) number.push_back(i); for (int j = 4; j >= 0; j--) number.push_back(j); if (IsPalindrome( word.begin(), word.end())){ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is a palindrome." << endl << endl; } else{ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is not a palindrome." << endl << endl; } if (IsPalindrome(number.begin(), number.end())){ vector<int> Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is a palindrome." << endl; } else{ vector<int> Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is not a palindrome." << endl; } system( "pause" ); return 0; } ostream & operator<<(ostream & stream, vector<int> & vec) { for(int i = 0; i < vec.size(); i++) cout << vec; return stream; }[/pre2] The vector should hold objects of type char. When the sequence is finished being read in, create two iterators. One iterator points to the beginning and the other the end of the vector. does my start and end correct? hehe

Сыроежка: For std::vector<int> you could use the simple comparision below instead of function IsPalindrome v == std::vector<int>( v.rbegin(), v.rend() )

Felicia123: but my question wanted me to use 2 pointer . 1 vector and 2 iterator. which the iterator is point to begin and point to end data . and the code seem like wrong? v== vector<int>( v.rbegin() , v.rend() ) it should insert at which place?

Сыроежка: For example instead of if (IsPalindrome(number.begin(), number.end())){ you could write if ( number == std::vector<int>( number.rbegin(), number.rend() ) ) { That is there is no need in any call of functon IsPalindrome

Felicia123: i did like yours for my last program. but my friend told me that it's not fullfill the question? or he is the one that give me wrong information? The vector should hold objects of type char. When the sequence is finished being read in, create two iterators. One iterator points to the beginning and the other the end of the vector. because i also thought that begin and end is already create two iterator? isn't we are correct and my friend is wrong?

Сыроежка: In your program you use a vector of type int. In the cited assignment there is said about of a vector of type char. As for iterators then member function end() returns the iterator that points beyond the last element of a vector. I do not know what your friend meant.

Felicia123: so am i wrong? I saw that u told me i have to use a vector of type char

Сыроежка: I do not know whether you are wrong. It will be said by the tutor who gave your the assignment.:)

Сыроежка: If to follow the assignment text Write a C++ program that reads a string/sequence from standard input till EOF. The string/sequence, including all white space, punctuation and newline characters should be read in character-by-character and stored into a vector. The vector should hold objects of type char. When the sequence is finished being read in, create two iterators. One iterator points to the beginning and the other the end of the vector. then the code could look the following way [pre2] #include <iostream> template <typename BidirectionalIterator> bool IsPolindrome( BidirectionalIterator first, BidirectionalIterator last ) { while ( first != last && first != --last && *first == *last ) ++first; return ( first == last ); } int main() { std::vector<char> v; std::istream::int_type c; std::cout << "Enter a sequence of characters: "; while ( ( c = std::cin.get() ) != EOF ) { v.push_back( c ); } if ( !v.empty() && v.back() == '\n' ) v.pop_back(); std::copy( v.begin(), v.end(), std::ostream_iterator<char>( std::cout ) ); std::cout << std::endl; std::cout << "The sequence is " << ( IsPolindrome( v.begin(), v.end() ) ? "" : "not " ) << "a polindrome\n"; return ( 0 ); }[/pre2]

Сыроежка: The code you showed shall not be compiled because you forgot to speciify header <vector> and you declared vector v twice. Update the code that we can discuss a correct code.:)

Сыроежка: Also take into account that there is statement if ( !v.empty() && v.back() == '\n' ) v.pop_back(); in my original code. It is important. Let assume that the user entered a sequence and pressed ENTER key and only after that he entered Ctrl+Z and ENTER (for EOF). In this case the vector will contain additional symbol of the new line that will prevent to determine correctly whether the entered sequence is a polindrome.

Felicia123: [pre2]#include<iostream> #include <vector> #include<string> #include<stack> #include<queue> using namespace std; int main(){ stack<char> s; queue<char> q; int i = 0; char InputChar; //Vector holds an object type char vector<char> v; istream::int_type c; cout << "Enter a sequence of characters: "; while ( ( c = cin.get() ) != EOF ) { v.push_back( c ); } vector<char> v1(v.begin(),v.end()); vector<char> v2(v.rbegin(),v.rend()); for( i = 0 ; i < v.size() ; i++ ){ s.push(v1); } for( i = 0 ; i < v.size() ; i++ ){ q.push(v2); } if ( !v.empty() && v.back() == '\n' ) v.pop_back(); [/pre2] here is my current cuode that changed as you mentioned and i slightly know that my program when compare with top and front() it should be have something like this while(!q.empty() && !s.empty()){ if(q.front() != s.top()){

Сыроежка: You shall place this statement [pre2] if ( !v.empty() && v.back() == '\n' ) v.pop_back(); [/pre2] before filling the stack and the queue and other vectors.



полная версия страницы