Dynamic Array Question

Hi, my question to you all is how do I store values into a dynamic array?! If I have this chunk of code:

1
2
3
4
5
6
7
8
9
10
11
12
while (break_apart >> word)
  {
    front = &word.at(0);
    rear = &word.at(word.size() - 1);
    while (front <= rear)
    {
          swap (*front, *rear);
          front++;
          rear--;
    }
    cout << word << " ";
  }


(If you were wondering about what this code is supposed to do --> it should input "Hello" and output "olleH", and this chunk is the meat of the program)

So, how would I store each newly modified word into a dynamic array? If you need more of my code, just let me know and I can post it up. Thank you to all who help in advance!!
Any reason why you can't use a vector (of strings) ?
how would it look if it were a vector of strings?
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!!

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
30
31
32
33
34
35
36
37
38
39
40
#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 << array << " ";
  }

  return 0;
}
void swap (char *front, char *rear)
{
     char temp = *front;
     *front = *rear;
     *rear = temp;
}


Thanks again!
Here is one way:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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);
   
  }
  
  //print out the vector
  vector<string>::iterator itr; //declare an iterator of the right type
  //loop through array vector using iterator
  for (itr = array.begin(); itr != array.end(); ++itr)
  {
    cout << *itr << " ";
  }
    
    
  return 0;
}
void swap (char *front, char *rear)
{
     char temp = *front;
     *front = *rear;
     *rear = temp;
}
i get an error message:

`array' cannot be used as a function


Also, because .begin and .end were supposedly not declared, i changed those, and this is the error message i get...thanks though for the help! I was messing around with the code, going with what I know (i'm not too familiar with vectors), so I tried out some with a dynamic array of type char and this is what i got, but i don't know what to put in the for loop...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
typedef char* ArrayPtr;
  ArrayPtr array;
  array = new char[1000];
  istringstream break_apart(input); // Puts the sentence into a stream
  
  while (break_apart >> word)
  {
    front = &word.at(0); //Assigns leading pointer variable to the first character of the word
    rear = &word.at(word.size() - 1); //Assigns ending pointer variable to the last character of the word
    while (front <= rear) //Allows both even AND odd length words to be used without complications
    {
          swap (*front, *rear);
          front++;
          rear--;
    }
    cout << word << " ";
  }

  for (int i = 0; i < input.length(); i++)
  {
      //WHAT DO I PUT HERE?!?
  }


Thanks again for your help!
Did you copy my code correctly - it certainly works.
oh yeah, you're right, sorry. can you explain what you coded from line 33 to 39 please? thanks
will do later - must go and do the day job now - and it looks as if it going to be an annoying pain in the a*se day.
haha, ok, thanks!
Are you acquainted with the c++ Standard template Library?
no i am not, do tell ;)
In order to be a better program you have to be somewhat self reliant.
Usually when I see something that I don't know about, I use the handy search engine called google.

STL includes maps, vectors, lists, deques, etc. for storing and manipulating data. Look at the reference section on this site for more information.
For an introduction to vectors, refer to the c++ reference on this very site (saves me typing)
http://www.cplusplus.com/reference/stl/vector/


An iterator is a STL object that behaves like a pointer.
Consider the following standard C/C++ code:

1
2
3
4
5
6
7
8
9
   #define ARRAY_MAX 5
   int  int_array[ARRAY_MAX] = {1,2,3,4,5}; //an array
   int *int_ptr; // a pointer to iterate through the array
  for (int_ptr = int_array;  int_ptr != int_array+ARRAY_MAX;  ++int_ptr)
  {
    
    cout << *int_ptr << endl;
  }
  


we have an array. We want to loop through the array, so we declare a pointer of the correct type int *int_ptr; and set it to the start of the array int_ptr = int_array;.
Each time round the loop we get the current value at the pointer and do something with it cout << *int_ptr << endl;.
We make sure that we don't step past the end of the array int_ptr < int_array+ARRAY_MAX; and we update the iterator each time round the loop ++int_ptr

The C++ STL designers have developed a direct replacement template class for the standard array called the vector.
The vector has what is called an interator - which is as we have said is a type of pointer.
The vector has a class method called begin() which returns an iterator or pointer
to the first elemen in the vector.
It also has a method called end() which returns a pointer to the element which is ONE past the last element in the vector.
To get the elecment currently being pointed at the iterator we use the familiar *pointer
notation, and to increment the iterator we use the familiar ++pointer notation.

So your code using vectors would be :

1
2
3
4
5
6
7
  //print out the vector
  vector<string>::iterator itr; //declare an iterator of the right type - in this case string
  //loop through array vector using iterator
  for (itr = array.begin(); itr != array.end(); ++itr)
  {
    cout << *itr << " ";
  }



The vector is a direct replacement for the standard array and much better and safer to use.
There are other 'containers' int the STL, like queues, stack, lists,........
Does that make sense?
Last edited on
Topic archived. No new replies allowed.