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.
Related
I am receiving the following error while calling push_back() method on the Vector class object:
error: request for member 'push_back' in 'edges.std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(((std::vector<int>::size_type)a))', which is of non-class type 'int'
I am trying to create a graph using an adjacency list and it isn't working
#include <bits/stdc++.h>
using namespace std;
/****************************************/
/// INPUT / OUTPUT
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
/****************************************/
/// GLOBAL DECLARATIONS
int n, m;
const int nmax = 100005;
vector <int> edges;
/****************************************/
inline void readInput()
{
f >> n >> m;
for(int i = 1 ; i <= m ; ++ i)
{
int a, b;
f >> a >> b;
edges[a].push_back(b);
}
}
int main()
{
readInput();
return 0;
}
Sorry for the bad writing this is my first question!
edges[a].push_back(b);
edges[a] gets the a.th element from the vector. As you use a vector<int>, the value you get is an int. And you can't call push_back on an int, as the type int has no member function push_back.
push_back as the name said, pushes a new value at the end of a vector. So you have to use edges.push_back(b).
If your intention is to insert a new value at a given position, you have to use std::vector::insert
Try changing the edges[a].push_back(b) to edges.push_back(b).
And as bolov commented, please explain what are you trying to achieve with edges[a].push_back(b). You ought to call push_back() method directly on the Vector class object.
Checkout this answer, it may help you out:
https://stackoverflow.com/a/34608598/9339019
By doing like this you execute the push_back method on an int and not on a std::vector. Try it instead:
edges.push_back(b);
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.
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;
}
I was trying to use vector svec to store some string values. But when compiling at Dev C++ 5.6.1, the compiler reported an error of "no match for operator=". Why is there this error and how to fix it? Thanks.
#include<vector>
#include<iostream>
#include<string>
using namespace std;
int main() {
vector<string> svec[100];
for (int i = 0; i < 100; ++i) {
svec[i] = "ABC";
}
return 0;
}
[Error] no match for 'operator=' (operand types are 'std::vector >' and 'const char [4]')
edit: The problem is in vector svec[100]; Things go well after changing it to vector svec(100);
edit2: I'm curious what the compiler regards this following declaration. Is svec still declared as a vector?
vector<string> svec[100];
vector<string> svec[100]; is creating an array with 100 elements of std::vector<std::string>. That's valid syntax although it's a rather odd thing to do.
In its current form, svec[i] therefore refers to a std::vector<std::string>. Assigning "ABC" to that is not possible. That's why you get the error.
What you mean to write is vector<string> svec(100);. This is calling one of the vector constructors which pre-sizes the vector to have 100 elements.
It just happens that std::vector overloads the [] operator to give you element access. (A rather convenient analogue to pure arrays.) If you make the change, svec[i] will refer to a string which you can set in the way that you currently do.
Change this statement
vector<string> svec[100];
to
vector<string> svec( 100 );
That is instead of an array of 100 vectors you should define a vector of 100 elements.:)
Or you could write simply
vector<string> svec;
// svec.reserve( 100 );
for (int i = 0; i < 100; ++i) {
svec.push_back( "ABC" );
}
And the most simplest way is the following
int main() {
vector<string> svec( 100, "ABC" );
return 0;
}
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.