-
@anon, what do you mean by store the words in a vector. My friend told me to store it in a dynamic array, but then he just left...can you show me a dynamic array example please?! Thank you for your help, oh and concerning your last post, I totally changed my program and I fixed the portion. I ultimately used what you said. Thanks though. Please let me know about the dynamic array.
-
std::vector is a container that provides a dynamic array.
-
Also, you don't need to have two code branches. If the string has an even number of chars, each gets swapped, otherwise, all but the middle does, which is the correct behavior anyway.
-
how do I store my values into a dynamic array?!
-
Dynamic Array Question
here is my code so far for those who would like some context to my previous post:
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
void swap (char *front, char *rear);
using namespace std;
int main()
{
string input, word;
char *front, *rear;
char *p; // This will hold the individual words temporarily until they are replaced
cout << "Enter a sentence that you would like to reverse: ";
getline(cin, input);
// put the sentence into a stream
istringstream break_apart(input);
while (break_apart >> word)
{
front = &word.at(0);
rear = &word.at(word.size() - 1);
while (front <= rear)
{
swap (*front, *rear);
front++;
rear--;
}
cout << word << " ";
}
return 0;
}
void swap (char *front, char *rear)
{
char temp = *front;
*front = *rear;
*rear = temp;
}
If you didn't see my previous post, I would like to know how to create a dynamic array. Thank you to all who can help me, much appreciated!
-
As Laserlight already pointed out, you can simply use std::vector. Try looking up the documentation for using one - that will probably help you solve these sort of issues yourself, in the future.
-
ok, so if it were a vector of strings, I would want to .push_back each word to store it at the end of the vector correct? essentially copying the components. so, I think I did that correctly, the code is posted below, but how would I display that vector of strings. the way I have obviously doesn't work. please let me know. Thank you for all of the help!!
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
void swap (char *front, char *rear);
using namespace std;
int main()
{
string input, word;
char *front, *rear;
vector <string> array;
cout << "Enter a sentence that you would like to reverse: ";
getline(cin, input);
istringstream break_apart(input);
while (break_apart >> word)
{
front = &word.at(0);
rear = &word.at(word.size() - 1);
while (front <= rear) {
swap (*front, *rear);
front++;
rear--;
}
array.push_back(word);
cout << word << " ";
}
return 0;
}
void swap (char *front, char *rear) {
char temp = *front;
*front = *rear;
*rear = temp;
}
-
You could do:
Code:
for( vector< string >::const_iterator next = array.begin( ), end = array.end( ); next != end; ++next )
cout << *next << endl;
Or perhaps:
Code:
for( size_t index = 0, size = array.size( ); index < size; ++index )
cout << array[ index ] << endl;
-
is there a way though, that I can input all of the characters of the modified string above into a string, i.e., so that I can just do cout << mod_string, and I see "olleH"? Thanks again for the help.
-
That's exactly what it does.
-
ok, maybe im confused. so with the example you gave me, i'll use the second one because I understand that one, i don't see how that's storing the values into an array. I see that for loop shooting out characters at the user one by one, rather than modified word by modified word. What i am looking for (because I do not know how to do it and I can't find a good example on the internet over dynamic arrays) is a way for my modified words to be put into a vector <string> or something like that one by one as they are modified. Then after the entire original string is read from, I can just say "cout << mod_string" without having to go through a for loop to do it.
Not to be rude or anything Sebastiani, maybe I'm wrong, it just doesn't look like that's what your post above does. Maybe I gave bad information so that you got the wrong idea, I don't know. Please let me know if I'm wrong though, because I do really want to understand this stuff. I appreciate your help, thanks again!
-
>> i don't see how that's storing the values into an array.
Either you're not paying attention or you're just plain confused. The examples I gave you output *strings* from a vector, which is what you had asked. Naturally, since the code you posted earlier already does the reading and storage of the words I assumed you didn't need yet another demonstration of how that was done.
-
a little bit of both now, haha. I apologize for the confusion.