How to modify a vector member's value? - c++

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;
}

Related

I am trying to push each letter of a word into a vector, but it is throwing an error

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?.

How to make a list of stacks in C++?

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.

C++ How to add vector in a vector? vector.push_back( vector<> )?

I'm creating a 2d vector array of char as a class variable, but I'm having troubles of adding a vector into the vector< vector > array.
I'm running gcc with C++ 11 standard.
I tried using vector< vector<char> > row(size, vector<char> ); but errors will show saying I did not define size and vector<char>. If I define it with int size and vector<char> col it will think it's a new function declaration since I'm putting it as a class variable.
So I use the usual method vector< vector<char> > row; and have a function to add another vector into it. So I use this->row[i].push_back( vector<char> col); but then an error saying error: expected primary-expression before ‘col’ appears. I also tried to use just this->row[i].push_back( vector<char> ); but the error is still there saying error: expected primary-expression before ‘)’. This might be dumb but I have no idea how to add a vector into a vector.
class Vector2d {
private:
int size;
vector< vector<char> > row;
public:
void make2d();
};
void Vector2d::make2d() {
for (int i = 0; i < this->size; i++) {
this->row[i].push_back( vector<char> col ); // compile error here
for (int j = 0; j < this->size; j++) {
this->row[i][j];
}
}
cout << "It works!" << endl;
}
I expect it to add the vector array into the vector array, and that I can use row[i][j] for the rest of the program. But it gives a compiler error saying error: expected primary-expression before ‘col’ I have no idea what to do.
vector<char> col is not a valid expression. It almost looks like a declaration of a variable (which is not what you need here), except it lacks a semicolon to complete the statement.
Furthermore, you cannot access row[i] until row actually contains at least i elements. row[i].push_back attempts to push a char element into a std::vector<char> that is the ith element of the vector of vectors (except you never created the ith element).
Given that in the following loop you appear to assume that the inserted vector should have size elements, here is how to insert a single vector of size elements into a vector of vectors:
row.push_back(std::vector<char>(size));
// or more simply
row.emplace_back(size);
If you would like to insert an empty vector instead, simply use:
row.emplace_back();
And here is how you can insert all size vectors of size elements in one go so that you can use row[i][j] for all i and j that are less than size:
row.resize(size, std::vector<char>(size));

Why is below program giving me error when I am assigning true or false to a vector of type bool?

I was doing a problem on TopCoder for finding the longest path in an acyclic directed graph. I have used vector of type bool for visiting vertices. But it is giving me these errors (highlighted in the code below):
error: no match for ‘operator=’ (operand types are ‘std::vector<bool>’ and ‘bool’)
visited[cur_ver]=true;
error: no match for ‘operator==’ (operand types are ‘std::vector<bool>’ and ‘bool’)
if(visited[i]==false)
Here is my code:
#include<bits/stdc++.h>
using namespace std;
class Circuits{
vector<int>adj[51];
vector<int>cost[51];
vector<int>T[51];
vector<bool>visited[51];
vector<int>dist[51];
int glm=0;
public:
void topological_sorting(int cur_ver,int n){
visited[cur_ver]=true; //error 1
for(int i=0;i<adj[cur_ver].size();i++){
if(visited[i]==false) //error 2
topological_sorting(i);
}
T.insert(T.begin(),cur_ver);
}
void Longest_Path(int s,int n){
for(int i=0;i<=n;i++)
dist[i]=NINF;
dist[s]=0;
for(int i=0;i<=n;i++){
int u=T[i]
if(dist[u]!=NINF)
for(int j=0;j<adj[i].size();j++){
int v=adj[u][j];
if(dist[v]<dist[u]+cost[u][v])
dist[v]=dist[u]+cost[u][v];
}
}
for(int i=0;i<=n;i++)
if(dist[i]>glm)
glm=dist[i];
}
int howLong(vector<string>connects,vector<string>costs){
for(int i=0;i<connects.size();i++){
for(int j=0;j<connects[i].size();j++){
adj[i].push_back(int(connects[i][j]));
cost[i].push_back(int(costs[i][j]));
}
}
int n=connects.size();
for(int i=0;i<=n;i++)
visited[i]=false;
topological_sorting(0,n);
int lm=0;
for(int i=0;i<=n;i++){
Longest_Path(i,n);
if(glm>lm)
lm=glm;
glm=0
}
return lm;
}
};
You are confusing built-in array syntax and syntax of the std::vector<T> class. Precisely, with
std::vector<int> myVec[51]
you declare and array of 51 vectors of type int. Thus, the code
visited[cur_ver]=true;
means "take element 52 of the array myVec, and assign true to it". However, that element is not of type bool, but of type std::vector<int>. There is no operator that would allow to assign bool values to a vector object.
To specify the size of a vector, which is your intention, use the appropriate constructor or the resize() method. When you're sure that the size of your container is fixed and known at compilation time, you may use std::array<size_t, T>, which is a fixed-length array container, available in C++11. But this is not the case with your code - you insert elements later on.
Thus, to fix the error, fix the syntax for vector declarations, and then resize in your class' constructor:
vector<int> adj;
// other vectors follow ...
// in Circuit::Circuit:
Circuit:Circuit() {
adj.resize(51); // others follow ...
}
Be sure to replace the 51 with a properly named constant. "Magic" constants are evil!
Vectors are like dynamic arrays with an ability to resize. We've only two ways of the declaration of the vector in c++.
vector <int> myVector;
vector <int> myVector2(4,1000);
The second will initialize a vector of size 4 with initial value 1000. If you want to provide a size maybe you can use like this.
vector <int> myVector(12);

Storing a string into a vector

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;
}