-1

I'm trying to insert the characters of the string into a char vector but place the letters in reverse order . can anyone tell me why this doesn't work

int main()
{
    string a = "Hello";

    vector<char> arr(5);

    for(int i = 4 ; i  ==   0 ; i--)
    {
        arr.push_back(a[i]);
        cout << arr[i];

    }
    return 0;
}

im trying to push back the character in reverse order 1 by 1

Galik
  • 47,303
  • 4
  • 80
  • 117
  • make sure when posting a problem that you specify what the current undesirable behavior is, too. – xaxxon Aug 14 '18 at 03:03
  • 2
    *I'm trying to push back the character in reverse order 1 by 1* -- Why 1 by 1? Do the whole thing at once: `vector arr(a.rbegin(), a.rend());` – PaulMcKenzie Aug 14 '18 at 04:14
  • You shouldn't keep asking new questions after your original question is answered. It invalidates the answers. Just accept the best answer then open a new question if needed. – Galik Aug 14 '18 at 05:14
  • i dont see how your answer helps here @Galik , thanks paulMckenzie that was useful! – jeffrey pernia Aug 14 '18 at 14:33

3 Answers3

3

Their are several problems with your code:

  • you are creating a vector whose size is initially 5, and then you are attempting to push 5 additional chars into it, for a total of 10 chars. You need to either:

    • initialize it's capacity instead of its size.

    • initialize the size as you are, but use arr[4-i] instead of arr.push_back() inside your loop.

  • your loop is never entered at all, since i == 0 is never true.

Try something more like this instead:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string a = "Hello";
    size_t len = a.size();

    vector<char> arr;
    arr.reserve(len);

    for(int i = len-1; i >= 0; i--) {
        arr.push_back(a[i]);
    }

    for(size_t i = 0; i < len; ++i) {
        cout << arr[i];
    }

    return 0;
}

Alternatively:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string a = "Hello";
    size_t len = a.size();

    vector<char> arr(len);

    for(int i = len-1; i >= 0; i--) {
        arr[len-1-i] = a[i];
    }

    for(size_t i = 0; i < len; ++i) {
        cout << arr[i];
    }

    return 0;
}

Another way to deal with this in a more C++-ish way is to use iterators instead:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string a = "Hello";

    vector<char> arr;
    arr.reserve(a.size());

    for(auto iter = a.rbegin(); iter != a.rend(); ++iter) {
        arr.push_back(*iter);
    }

    for(auto ch : arr) {
        cout << ch;
    }

    return 0;
}

Alternatively:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string a = "Hello";

    vector<char> arr(a.size());
    auto arr_iter = arr.begin();

    for(auto a_iter = a.rbegin(); a_iter != a.rend(); ++a_iter, ++arr_iter) {
        *arr_iter = *a_iter;
    }

    for(auto ch : arr) {
        cout << ch;
    }

    return 0;
}

And then you can get rid of the manual loops altogether:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
    string a = "Hello";

    vector<char> arr;
    arr.reserve(a.size());

    copy(a.rbegin(), a.rend(), back_inserter(arr));

    cout.write(arr.data(), arr.size());

    return 0;
}

Alternatively:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    string a = "Hello";

    vector<char> arr(a.size());

    copy(a.rbegin(), a.rend(), arr.begin());

    cout.write(arr.data(), arr.size());

    return 0;
}

Alternatively:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string a = "Hello";

    vector<char> arr(a.rbegin(), a.rend());
    cout.write(arr.data(), arr.size());

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

I'm not sure why you're complicating matters when a vector is perfectly capable of taking an iterator in the constructor? The vast majority of your code can therefore be replaced with a simple:

vector<char> arr(a.rbegin(), a.rend());

The complete program below shows this in action:

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::string; using std::vector;

int main() {
    string a = ")-: yug emosdnah a si xaP";
    vector<char> arr(a.rbegin(), a.rend());
    for (auto ch: arr) cout << ch;
    cout << '\n';
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0
for(int i = 4 ; i  ==   0 ; i--)

That means to keep going while i is equal to zero - but i starts out at four, so the for loop terminates immediately.

You probably meant

for(int i = 4 ; i  >=   0 ; i--)
xaxxon
  • 19,189
  • 5
  • 50
  • 80