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];
}
}
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.
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.
Because you are trying to put in char into a string vector.
Change input to string.
#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;
}
Related
This is my code:
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
int main()
{
string word = "hi";
int i = 0;
vector<string> vec;
for(i ; i < word.length() ; i++)
{
vec.push_back(word[i]);
}
return 0;
}
I am trying to push each letter of string "hi" into a vector vec using for loop, but, it is throwing an error on the 14th line: "error: no matching function for call to ‘std::vector >::push_back(char&)’"
Indexed access ([] operator) on std::string returns char& and not std::string itself.
Vector declared is vector of string so insertion into vector requires string type not char&
If you want to have vector of char, change accordingly
You get the error because you have a vector of strings, not characters.
Change it to a vector of characters and it should work:
std::vector<char> vec;
As for what you're trying to do, there is a simpler way to do it:
std::vector<char> vec(begin(word), end(word));
The error lies in the fact that you have a std::vector of std::string's, but you are trying to push_back a char. There are multiple solutions to this:
Change your std::vector<string> to a std::vector<char>
Create a new std::string object every time you want to push_back a char like this (This is using an overloaded std::string constructor but there are many more possibilities.):
vec.push_back(string(1,word[i]));
Note: Please consider reading about Why is “using namespace std;” considered bad practice?.
I'm trying to make a list of stacks in C++ using the code below , but I'm getting the error
main.cpp:17:13: error: ‘__gnu_cxx::__alloc_traits > >::value_type {aka class std::stack}’ has no member named ‘push_back’
vs[i-1].push_back(s);
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<stack<int>> vs;
for(int i=1; i<4; i++)
{
stack<int> s;
s.push(i*2);
s.push(i*3);
s.push(i*4);
vs[i-1].push_back(s);
}
return 0;
}
you can't use this line:-
vs[i-1].push_back(s);
Either define the size of the list earlier.
For Eg
vector<stack<int> > vs(100);
else only write
vs.push_back(s);
Updated Solution
#include <iostream>
#include<stack>
#include<vector>
using namespace std;
int main()
{
vector< stack<int> > vs;
for(int i=1; i<4; i++)
{
stack<int> s;
s.push(i*2);
s.push(i*3);
s.push(i*4);
vs.push_back(s);
}
return 0;
}
First of all, your vector is empty, any indexing in it will be out of bounds and lead to undefined behavior.
Secondly, vs[any_valid_index] is a stack and not a vector.
What you probably want is
vs.push_back(s);
Its not a fact for only vector of stacks. It is actually basic property of vector. You have two options to add value into vector.
If you don't declare size of vector (what you did) like
vector<stack<int>> vs;
stack<int>s;
Then you can insert by using vs.push_back(s) It will automatically handle the index.
Another way is you can declare the size of the vector first. like
vector<stack<int>> vs(sz); //size(sz) is defined by user
stack<int>s;
Then you can insert by using vs[index]= s This time you have to manually handle the index.
What is the difference between the two wording when the two-dimensional array is a function parameter?
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
void dfs(int (*a)[10]){
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
cout<<a[i][j]<<" ";
}
}
}
void dfs2(int a[][10]){
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
cout<<a[i][j]<<" ";
}
}
}
int main(){
int (*a)[10]=new int[5][10];
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
a[i][j]=i*j;
}
}
dfs(a);
dfs2(a);
delete []a;
return 0;
}
dfs(int (*a)[10]) and dfs2(int a[][10]) all can work,I want to know when the array very large,Will that be efficient?
What is the difference between the two wording when the
two-dimensional array is a function parameter?
There is none.
That's also why you get a redefinition error if you try to give both functions the same name.
Nevertheless, the dfs(int (*a)[10]) version is of greater eductional value, because it shows you that arrays cannot be passed by value; when you attempt to do so, a pointer is passed instead and the size information is lost. You end up with a pointer to an int[10] object, which may or may not be the beginning of a whole array of int[10]s.
I want to know when the array very large,Will that be efficient?
As there is no difference at all, it also doesn't matter how large your arrays are. Efficiency is a completely unrelated topic, too.
In any case, drop all the array stuff and use std::vector. And don't use the non-standard <bits/stdc++.h> header, and don't use <windows.h> if you don't need the Windows API. And finally, avoid using namespace std;.
I am getting segmentation error in the following code, can anyone bother to explain. I think it might have to do with initialization, but not sure. I am just trying to clone the existing stack and perform operation such as adding entry to the clone or removing entry from existing and cloning it to new stack.
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <string>
using namespace std;
#define in cin
#define out cout
int main()
{
//ifstream in("postfix.in");
//ofstream out("postfix.out");
int n;
in>>n;
long sum=0;
vector<int> tm(0);
vector<vector<int>> ar(0,tm);
//ar[0].push_back(0);
out<<ar[0][0];
for(int i=0;i<n;i++)
{
int ind,val;
in>>ind>>val;
if(val==0)
{
for(int j=0;j<ar[ind-1].size();j++)
ar[i].push_back(ar[ind-1][j]);
ar[i].pop_back();
}
else
{
for(int j=0;j<ar[ind-1].size();j++)
ar[i].push_back(ar[ind-1][j]);
ar[i].push_back(val);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<ar[i].size();j++)
sum+=ar[i][j];
}
out<<sum<<endl;
return 0;
}
vector<int> tm(0);
vector<vector<int>> ar(0,tm);
Here you initialized ar as an empty vector of vector of int. Without enlarging its size through push_back(), resize(), insert(), etc., you cannot access ar[i].
You may instead initialize ar as
vector<vector<int>> ar(n);
But in the existing snippet you provided there is no clue about how large the second dimension should be.
Per your comment in this answer, your declaration of tm and ar should be
vector<int> tm(1, 0);
vector<vector<int>> ar(1, tm);
Or even shorter since tm is not really used later,
vector<vector<int>> ar(1, vector<int>(1, 0));
I think I see where you're getting tripped up.
vector<int> tm(0);
This does not create a vector containing a 0. This creates a vector with a size of 0. Because this list has a size of 0, you can't get the first element; since it's empty!
Same here:
vector<vector<int>> ar(0,tm);
This doesn't create a vector with 1 "row". It creates an empty vector, since you made the size 0 again.
You likely intended something like:
vector<int> tm(1, 0);
vector<vector<int>> ar(1,tm);
Which creates a row, tm, with a single 0, then creates a 2D vector, ar containing that 1 row.
Check the reference. You're attempting to use the "fill" constructor.
For starters it is a bad idea to use such definitions
#define in cin
#define out cout
It is better to use explicitly std::cin and std::cout because any programmer knows what these names mean.
These declarations
vector<int> tm(0);
vector<vector<int>> ar(0,tm);
do not make great sense. It would be much clear and simpler just to write
vector<int> tm;
vector<vector<int>> ar;
SO as the vectors are empty you may not use the subscript operator as you are doing for instance here
out<<ar[0][0];
^^^^^^^^^
for(int i=0;i<n;i++)
{
int ind,val;
in>>ind>>val;
if(val==0)
{
for(int j=0;j<ar[ind-1].size();j++)
^^^^^^^^^^^
ar[i].push_back(ar[ind-1][j]);
^^^^^
ar[i].pop_back();
^^^^^
}
and so on. You need at first ro append new elements to the vector before using the subscript operator.
I have an array of strings which need to be taken into a map. Since the array size is variable, I need a 2d vector to obtain the string character-wise. i need both formats of storage for operations i perform on them. Here's my attemp..gives errors in (EDIT:)run time.
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<map>
#include<vector>
#include<algorithm>
#include<iterator>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
using namespace std;
std::map<int,string>col;
std::map<int,string>row;
std::map<int,string>::iterator p;
std::map<int,string>d1;
std::map<int,string>d2;
int main()
{
int i=0,r=0;
string s;
ifstream ip;
ip.open("a.in");
ofstream op;
op.open("a_out.in");
ip>>s;
const int c= s.length();
ip.seekg(0,std::ios::beg);
do {
ip>>s;row.insert(make_pair(r,s));
r++;
}while(s.length()==c);
p=row.find(--r);
row.erase(p);
p = row.begin();
while(p!=row.end())
{
cout<<(p->first)<<","<<(p->second)<<"\n";
p++;
}
vector<vector<char>>matrix(r,vector<char>(c));
rep(i,0,r){
int k=0;rep(j,0,c)(p->second).copy(&matrix[i][j],1,k++);
}
rep(i,0,r)
rep(j,0,c)
cout<<matrix[i][j];
return 0;
}
It looks like the problem occurs after you print out the map, before you copy the strings into the vector. You need two things:
while(p!=row.end())
{
cout<<(p->first)<<","<<(p->second)<<"\n";
p++;
}
p = row.begin(); // Must reset iterator!
vector<vector<char>>matrix(r,vector<char>(c));
rep(i,0,r){
int k=0;
rep(j,0,c)(p->second).copy(&matrix[i][j],1,k++);
++p; // Must advance the iterator.
}
That should fix the map/set iterator not dereferencable, as in the doubly nested for loop you referenced an invalid iterator (p was set to row.end()).
Edit:
Also, unless you can assume that all the strings are the same length, you might consider a different technique. When you use const int c = s.length(), you are telling the map<int,string> and vector<char> the length of EVERY string in your file are the exact same length. If the second string is shorter than the first, you will try to access characters in the string that don't exist! Note the
rep(j,0,c) (p->second).copy(&matrix[i][j],1,k++)
will fail since it thinks it has c characters, when it in fact will not.