81 Replies - 21126 Views - Last Post: 27 February 2013 - 11:18 PM
#46
Re: A little challenge: "Reverse a string"
Posted 05 March 2012 - 04:13 PM
#47
Re: A little challenge: "Reverse a string"
Posted 05 March 2012 - 08:42 PM
I was trying to think of a reason for that clause(other than not breaking legacy code) and I believe it's an optimization that allows using things like memcpy, memset, and memmove with iterators of containers that have this clause(one of the reasons std::vector is so fast). The idea is that there would be class template called something like 'contiguous_iter', containers like std::string and std::vector would use it, and the STL algorithms can use specializations, overloads or SFINE(not sure which would be used, maybe a combo) to select the fastest algorithm. I know VC++'s STL does this for std::vector; I bet it does the same thing with std::string sense they could easily have the same iterator implementation.
This post has been edited by ishkabible: 05 March 2012 - 08:44 PM
#48
Re: A little challenge: "Reverse a string"
Posted 06 March 2012 - 04:06 PM
#49
Re: A little challenge: "Reverse a string"
Posted 07 March 2012 - 10:48 AM
Ricky65, on 06 March 2012 - 06:06 PM, said:
In my opinion this statement is false. Bjarne Stroustrup says in his book "The C++ Programming Language: Special Edition" on page 589 that:
Quote
Maybe I'm interpreting it wrong, but hypothetically speaking if the string was implemented as lets say a linked list, it would still be possible to construct an array from that linked list and return a pointer to the first element of that array.
This post has been edited by vividexstance: 07 March 2012 - 10:49 AM
#50
Re: A little challenge: "Reverse a string"
Posted 07 March 2012 - 06:32 PM
#51
Re: A little challenge: "Reverse a string"
Posted 08 March 2012 - 01:22 PM
#52
Re: A little challenge: "Reverse a string"
Posted 08 March 2012 - 02:05 PM
OK, since I guess I was the perpetrator of this silly idea ...
I don't think we would want to have a linked list and a "permanent" array since linked list was intended to avoid an array in the first place. To satisfy c_str() we need a const array, but it only needs to be const until the next non-const method is called. So how about a dynamically allocated array that's initialized when c_str() is called (only if it isn't already non-null, as would be the case if c_str() is called multiple times with no intervening non-const method calls), and deleted by calling a private "destroyer" method whenever any non-const method is called.
This post has been edited by r.stiltskin: 08 March 2012 - 02:09 PM
#53
Re: A little challenge: "Reverse a string"
Posted 11 March 2012 - 01:59 PM
vividexstance, on 07 March 2012 - 10:48 AM, said:
Ricky65, on 06 March 2012 - 06:06 PM, said:
In my opinion this statement is false. Bjarne Stroustrup says in his book "The C++ Programming Language: Special Edition" on page 589 that:
Quote
Maybe I'm interpreting it wrong, but hypothetically speaking if the string was implemented as lets say a linked list, it would still be possible to construct an array from that linked list and return a pointer to the first element of that array.
Sure, we could have a member function of a string class implemented with a linked list that returns a pointer to the first element in the relevant array.
However, the data() and c_str() member functions are usually passed to C functions which expect a contiguous built-in C array. This is the reason why only the contiguous collections, std::vector, std::array and std::string have the data() member function.
I would also like to add that since C++11, data() and c_str() perform the same function. data(), like c_str(), now appends a terminating null character.
#54
Re: A little challenge: "Reverse a string"
Posted 18 March 2012 - 06:53 AM
#55
Re: A little challenge: "Reverse a string"
Posted 18 March 2012 - 08:22 AM
This post has been edited by vividexstance: 18 March 2012 - 08:24 AM
#56
Re: A little challenge: "Reverse a string"
Posted 29 March 2012 - 08:31 PM
Of course this only works for a 4-char string, but it was still fun
#include <iostream>
/* 4 character stack */
class char_pack {
unsigned int data;
public:
char_pack()
: data(0) {
}
void push(char c) {
data = (data << 8) | c;
}
char pop() {
char c = data & 0xFF;
data = data >> 8;
return c;
}
void print() {
char c;
if((c = pop()) != 0) {
std::cout<<c;
print();
}
}
};
int main() {
char_pack str;
str.push('t'); str.push('e'); str.push('s'); str.push('t');
str.print(); //prints stack, which is the reversed string
std::cin.ignore();
std::cin.get();
return 0;
}
This post has been edited by jjl: 01 April 2012 - 02:26 PM
#57
Re: A little challenge: "Reverse a string"
Posted 30 March 2012 - 06:41 AM
#58
Re: A little challenge: "Reverse a string"
Posted 31 March 2012 - 10:25 PM
#59
Re: A little challenge: "Reverse a string"
Posted 22 April 2012 - 08:06 AM
#include <iostream>
using namespace std;
int main()
{
int j = 0;
int i;
cout << "Enter a word or a sentence: ";
string word;
getline(cin, word);
for (i = word.size() - 1; i >= 0; i--)
{
cout << word[i];
}
cout << "\n\n";
main();
}
But i need help with the palindrome. Anyone knows how to do it?
#60
Re: A little challenge: "Reverse a string"
Posted 23 April 2012 - 09:10 PM
Quote
Your using a string on line #10
|
|

New Topic/Question
Reply



MultiQuote








|