0

The error says that no matching function to call for push_back().

I included <vector> so I don't understand why this error is occurring. If you could also show me how to take in a string and store it into a vector that would be really helpful!

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

int main()
{
    vector<string> list;
    char input;
    while(cin>>input)
    {
        list.push_back(input);
    }
    for(int i=0;list.size();i--)
    {
        cout<<list[99-i];
    }

}
pizzaisdavid
  • 455
  • 3
  • 13
Brogrammer93
  • 97
  • 1
  • 2
  • 9

4 Answers4

6

Since your list is a vector of string, pushing single chars into it wouldn't work: you should either make it a vector of chars, or read strings:

string input;
while(cin>>input) {
    list.push_back(input);
}

Note that list[99-i] is rather suspicious: it will work only if the list has exactly 99 elements, and only if you change i-- for i++. Otherwise, you would get undefined behavior either on accessing elements past the end of the vector, or accessing elements at negative indexes.

If you would like to print the list from the back, use list[list.size()-1-i] instead, and use i++ instead of i--, because otherwise the loop would not stop.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I fixed my code but I'm still getting some errors: #include #include #include using namespace std; int main() { vector list; string input; while(cin>>input&& input!='\n') { list.push_back(input); } for(int i=0;i – Brogrammer93 May 10 '15 at 03:15
  • @Darraptor the comparison with `"\n"` should be removed ([demo](http://ideone.com/ntN9YX)). It's of no use anyway, because operator `>>` treats `'\n'` as a delimiter, so it wouldn't put it into a string. If you want to check for an empty line, use this condition: `in>>input&& input.size() != 0` – Sergey Kalinichenko May 10 '15 at 03:19
2

Well, the error is correct. It helps, though, to read all of it!

The class vector<string> has a function push_back(const string&).

It does not have a function push_back(char).

I don't know why you're extracting individual chars but storing whole strings; don't do that.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Because you are trying to put in char into a string vector. Change input to string.

0
#include<bits/stdc++.h>
using namespace std;


int main()
{
int n;
cin>>n;
string v;
vector<string> s;
for(int i=0;i<n;i++)
{
    cin>>v;
    s.push_back(v);
   
}

sort(s.begin(),s.end());
for(int i=0;i<n;i++)
{
    cout<<s[i]<<endl;;
   
}
return 0;

}
Knight
  • 1
  • 1
  • Recommending the use of `#include` in an answer on Stack Overflow is just asking for downvotes. See: [Why should I not #include ?](https://stackoverflow.com/q/31816095/10871073) Another good read: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/10871073) – Adrian Mole Oct 21 '22 at 07:30