guys, i have made this code for strings when i compile it shows no error it also runs but after displaying the strings the program sudddenly stops and displays the message"main.exe has stopped working windows is checking for solution.." how to fix this?
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<conio.h>
using namespace std;
int main(){
cout<<"vector creation program"<<endl;
string input;
cout<<"enter the strings"<<endl;
vector<string> v;
for(int m=0;m<10;m++)
{
getline(cin,input);
v.push_back(input);
}
int n=sizeof(v);
for(int i=0;i<n;i++)
{
cout<<v[i]<<endl;
}
}
The value returned by sizeof(v) is the number of bytes v occupies. It doesn't have any relation at all to the number of elements v stores, so it isn't suitable for limiting an index.
1 2 3
std::size_t n = v.size();for ( std::size_t i=0; i<n; ++i)
//...