0

I am trying to take two strings as input and combine them and all their individual character into a vector.

Full Code:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string str1;
    string str2;

    cin>>str1>>str2;

    vector<char>vec;

    for(int i=0;i<str1.length();i++){
        vec[i]=str1[i];
    }

    int k=str1.length();
    for(int i=0;i<str2.length();i++){
        vec[k]=str2[i];
        k++;
    }
    
    for(int i=0;i<vec.size();i++){
        cout<<vec[i];
    }
}

What am I missing? Am I missing a concept about strings here?

I am trying to take two strings as input and combine them and all their individual character into a vector.

In terminal code shows no error, takes input for both strings, but shows no output.

user16217248
  • 3,119
  • 19
  • 19
  • 37
  • 3
    Use `vec.push_back()` instead of `vec[i]` – Aamir Aug 11 '23 at 01:53
  • 4
    `vec` is empty. `vec[i]` exhibits undefined behavior for any value of `i`, by way of accessing an index out of bounds. – Igor Tandetnik Aug 11 '23 at 01:54
  • When you learn C++ use `vec.at(i)` instead of square brackets. – 273K Aug 11 '23 at 01:58
  • Whichever C++ textbook taught you to use `` -- you should throw it away and get a different C++ textbook. If you copied that off some web site, without any explanation, don't visit that web site any more. If you saw this in some clown's Youtube video, unsubscribe from that channel, you're not learning proper C++. This is not a standard C++ header file, many C++ compilers don't have it and will not compile the shown code. – Sam Varshavchik Aug 11 '23 at 02:00
  • @igortandetnik I now understand what I missed. Thank you . – Archit Sharma Aug 11 '23 at 02:41
  • @samVarshavchik i never used that header until today, felt lazy and wrote that. Never again.Thank you for insight. – Archit Sharma Aug 11 '23 at 02:48
  • 1
    Side notes: (1) [more info why I shouldn't use #include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) (2) [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – wohlstad Aug 11 '23 at 04:20
  • @wohlstad I will follow this practice from now on. Many thanks! – Archit Sharma Aug 12 '23 at 12:10

1 Answers1

2

You declare a vector of chars vec with no initial size. At this point, vec has a size of 0, meaning that it can't store any elements (without getting into too much detail). Then, you attempt to modify the vector with vec[i]=str1[i]. Since vec doesn't actually have any storage to store the elements, you're modifying memory you don't own. This is a segmentation fault, and kills the program. When a segmentation fault occurs, there is no output produced, hence why there isn't output.

You should allocate memory for vec using the method resize:

//...
vector<char> vec;
vec.resize(str1.length() + str2.length());

for (int i = 0; i < str1.length(); i++) {
//...

There shouldn't be a segmentation fault after this.

  • Didn't knew that information. I can't thank you enough for taking the time to share your knowledge on Stack Overflow. Many thanks! – Archit Sharma Aug 11 '23 at 02:52