I am writing this code
string largestNumber(const vector<int> &A) {
{
//Doing something
}
result.append(to_string(A[maxindex]));
A.erase(A.begin()+maxindex);
}
cout << result;
}
Now in this I am using the erase function correctly. Passing the iterator from the start and adding index value to it.So what am I doing wrong here?
The error comes
no matching member function for call to 'erase'
I have included the vector header as
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
"Now in this I am using the erase function correctly."
No you don't.
According the declaration described here
Type requirements
-T must meet the requirements of MoveAssignable.
you cannot modify a const referenced std::vector, erase() requires a non const reference, so what's actually unclear about the compiler error message?
You have to declare your function that it receives a non const reference parameter:
string largestNumber( /* const */ vector<int> &A) {
// ^^^^^ omit this
or make it eligible for moving
string largestNumber( vector<int> &&A) {
Related
So i have created a queue of stacks using the STL as follows:
void main()
{
queue<stack<string>> qos;
stack<string> words;
words.push("hey");
qos.push(wors);
cout<< (qos.pop()).top()<<endl;
}
Expected behavior:
returns the word hey
Actual result:
error: member reference base type 'void' is not a structure or union
My question is why doesn't it return what i was expecting, i mean since qos.pop() returns stack element and stack has the member function top();
Your variables words and qos are not related in any way.
Also main() must have a return type of int.
The reason for the error message you get is, that queue<>::pop does not return a value top() could be called upon.
You probably want
#include <iostream>
#include <queue>
#include <stack>
#include <string>
int main()
{
std::queue<std::stack<std::string>> qos;
std::stack<std::string> words;
words.push("hey");
qos.push(words);
std::cout << qos.front().top() << '\n';
}
Hey guys? I'm new to the whole C++ Standard Library thing, but for the devil of me, I cannot figure out why this program is not giving me the output I hope.
#include <iostream>
#include <vector>
#include <iterator>
/*
create_vec should initialise my vector and return an iterator pointing to
it.
*/
template <typename s>
typename std::vector<s>::iterator create_vec(s var) {
std::vector<s> tempVec;
tempVec.push_back(var);
auto itr = tempVec.begin();
return itr;
}
int main() {
std::vector<int>::iterator itr = create_vec<int>(148);
std::cout << *itr << "was passed." << std::endl;
return 0;
}
O/P : 0 was passed to the create vec function
p.s ignore all the std's. I want to know at each moment where i got each type, function etc.
The variable tempVec is local inside the create_vec function. When the function returns the vector object is destructed, leaving you with an iterator to something which doesn't exist any more. That leads to undefined behavior when you try to use the iterator.
Isn't the purpose of the create_vec function to return the vector instead? The name of the function suggests it should do so.
I am using C language to upgrade my coding skills.
I designed a simple program because I wanted to easily find the problems what I looked for and arrange the works when I handle many problems as shown in below.
Here is my header file
#pragma once
#ifndef PROBLEM_H
#define PROBLEM_H
namespace PROBLEM_1 { int do_main(); }
typedef int(*MAINFUNC)();
#endif
And below is my source file.
#include "problems.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
typedef int(*MAINFUNC)(void);
map<string, MAINFUNC> func_map;
void initialize_problem_map(void) {
func_map["problem_1"] = PROBLEM_1::do_main;
}
namespace PROBLEM_1 {
int do_main(void) {
cout << "hi" << endl;
return 1;
}
}
int main(void) {
string problem;
initialize_problem_map();
cin >> problem;
if (func_map.find(problem) != func_map.end())
return (*(func_map[problem]))();
return -1;
}
If I input "PROBLEM_1" then, the do_main functions in namespace PROBLEM_1 will be executed. I think that this design helps me organize multiple coding problems.
However, my question is about these two code lines as below.
if (func_map.find(problem) != func_map.end())
return (*(func_map[problem]))();
As you can see, the main fucntion's return type is "int". However, in the if-clause, i think that it return function-pointer. Therefore, I thought that those returning behavior is mismatched with the main function's return type. But, to my surprise, it worked well.
Could u explain this procedures regarding returning types?
func_map[problem], indeed, results in a function pointer. Applying the operator () on it, the function is invoked and the expression results in an int. Dereferencing a function pointer before its invocation is optional. This is symmetric with an optional address taking on a function name for initializing function pointers.
Indeed
func_map[problem]
is a pointer. However, you dereference the pointer with *:
*(func_map[problem])
and call function by adding ():
(*(func_map[problem]))()
which returns "int".
find returns an iterator; if this iterator is end, then the problem does not exist in the map; Because it is not end, it exists, then in the return line the pointer function obtained with [] is used to call it.
Why is it giving me an undefined reference to MySet::MySet() while trying to print my vector? Any help would be much appreciated. Thanks
#include <iostream>
#include <vector>
using namespace std;
class MySet{
public:
MySet();
MySet(const vector<int> v3);
void printVector(const vector<int>& newMyVector);
};
int main()
{
vector<int> myVector;
myVector.push_back(1);
myVector.push_back(2);
MySet m;
m.printVector(myVector);
return 0;
}
void MySet::printVector(const vector<int>& newMyVector){
cout << "Vector: ";
for(unsigned int i = 0; i < newMyVector.size(); i++)
{
cout << newMyVector[i] << "|";
}
}
Why is it giving me an undefined reference to MySet::MySet() while trying to print my vector?
You are creating an instance of MySet using
MySet m;
That uses the default constructor. The default constructor has been declared in the class but it has not been defined. You can fix it by defining it.
MySet::MySet() {}
Your program does not include a definition for the constructor of MySet. The constructor's name is MySet::MySet(). So when you try to create an instance of the object, the compiler does not know what to do. There are a couple solutions:
Remove the line right after "public:" that declares the constructor. If you don't declare any constructors, your object will just use a default constructor supplied by the compiler which might be good enough.
Or, add a definition for the constructor somewhere in your program.
Or, you can change the line that declares MySet::MySet() so that it also defines it.
The code for option 2 would look like this, somewhere outside the class:
MySet::MySet()
{
// do stuff here to construct object
}
The code for option 3 would look like this, somewhere inside the class:
MySet()
{
// do stuff here to construct object
}
In my class.h i got:
template<class T> void Swap(T, T);
And in class.cpp:
template<class T>
void class::Swap(T& p1, T& p2)
{
T aux = p1;
p1 = p2;
p2 = aux;
}
When I try :
this->Swap<char*>(&lit[i][k], &lit[i][l]);
And another question:
What am i doing wrong here: i want to split my string after some delimitators ("+-") and strtok isn't working as I expected.
int counter = 0;
char* s = strtok(eq_clone ," ");
while(s != NULL)
{
counter++;
if(s == "")
counter--;
s = strtok(eq_clone ,"+-");
}
This looks like a mistake as it will never be true:
if(s == "")
this is comparing the address of s to the address of the string literal "": it is not checking if s is an empty string. An alternative to using strtok() would be boost::algorithm::split:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
int main()
{
std::string s("a,string+with-multiple delimiters");
std::vector<std::string> s_tokens;
boost::algorithm::split(s_tokens, s, boost::is_any_of(",+- "));
std::for_each(s_tokens.begin(),
s_tokens.end(),
[] (const std::string& s)
{
std::cout << s << "\n";
});
return 0;
}
Output:
a
string
with
multiple
delimiters
Regarding swap, as has already been stated in comments and other answer(s) just use std::swap(). I am guessing that lit is a char[][] so std::swap(lit[i][k], lit[i][l]); will do exactly what you require.
EDIT:
After comment giving declaration string* lit and that lit appears to be an array (from example usage) then use std::swap() in this manner:
std::string* lit = new std::string[4];
lit[0] = "zero";
std::swap(lit[0][1], lit[0][2]); // The characters will be passed by reference,
// don't take address.
std::cout << lit[0] << "\n"; // Prints "zreo"
Note, that the declaration of your Swap() passes arguments by value (incorrect), but the definition of Swap() passes arguments by reference (correct). If you change the declaration of your Swap() and invoke it as follows it will work (if you really don't want to use std::swap():
template<class T> void Swap(T&, T&);
//^ //^
Swap(lit[i][k], lit[i][l]);
1) Why is Swap() a class member? Class members should somehow be tightly coupled to your classes. In most cases it is is sign of bad design if something, which does not use private members or is very similar to a method that does (i.e. a convinience method), becomes a class member. C++ is not java where everything has to belong to a class. Make your swap() method a free standing template method.
2) Better yet, do not reinvent the wheel. std::swap() is there and it works mightily well. In many cases you can expect the methods and classes provided by the standard library to work better than something you could write up.
3) In your class you called the method Sort(), but the question is about Swap(). Since you did not write what you expected to happen and what actually happens, this is the only thing I can find which might be wrong.
4) Do not use strtok() in C++ unless you have to. char* are C-Style strings and should not be used in C++. Use std::string instead.