having trouble passing lower_bound a fourth argument [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am not sure how to pass a comparison function to the lower_bound function. When I try to pass 4 arguments, I get an error. I would greatly appreciate an example of how to pass a comparison function in the lower_bound function.

In the overload of lower_bound you are using, Compare must meet the requirements of BinaryPredicate.
The example you have asked for can be found online.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool ignore_case(char a, char b) {
return(tolower(a) == tolower(b));
}
int main(void) {
vector<char> v = {'A', 'b', 'C', 'd', 'E'};
auto it = lower_bound(v.begin(), v.end(), 'C');
cout << "First element which is greater than \'C\' is " << *it << endl;
it = lower_bound(v.begin(), v.end(), 'C', ignore_case);
cout << "First element which is greater than \'C\' is " << *it << endl;
it = lower_bound(v.begin(), v.end(), 'z', ignore_case);
cout << "All elements are less than \'z\'." << endl;
return 0;
}
Demo here.

Related

Apply function that uses matrix elements to update attribute [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I have an Eigen::MatrixXd and I would like to take all the elements in the matrix to update an attribute // within the class. However when I run the following I get errors.
#include <cmath>
#include <iostream>
#include <Eigen/Core>
double x = 0;
void Increment(double x, double increment)
{
value = value + x + increment;
}
int main()
{
Eigen::MatrixXd m(2, 2);
m << 1, 1, 1, 1;
std::cout << m << std::endl << "becomes: ";
std::cout << std::endl << m.unaryExpr(&Exp(1)) << std::endl;
std::cout << value << std::endl << " is the new value";
}
Expecting to get a value of "8" for last cout statement

Which method is to access Pairs? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
just to clear this doubt of fine i just want to ask that which method is faster or more efficient to access pairs while iterating over a contiguous block of pairs.
I used two method to iterate over a block.
1st
pair<int, char> arr[3] = {{1, 'a'}, {2, 'b'}, {3, 'c'}};
for (int i = 0; i < 3;i++){
cout << get<0>(arr[i]) << " " << get<1>(arr[i]) << endl;
}
2nd
for(const auto &x:arr){
cout << x.first << " " << x.second << endl;
get<0>(arr[0]);
}
which one is better and more efficient pls explain if u can.
You can compare them here: https://godbolt.org/z/4dPzKaPWr
As you will see, both have the same assembly code.
Efficiency takes many forms. I don't expect huge runtime differences in either case (you will have to measure/profile and expect std::cout to slow things down a lot, so be careful what you measure!).
But in terms of maintainability (which is also an efficiency).
I would use structured bindings on pairs (or not use pairs at all and use my own struct with clearly readable names)
#include <iostream>
#include <utility>
// don't use : using namespace std
struct my_data_t
{
int number;
char character;
};
int main()
{
std::pair<int, char> arr[]{{1, 'a'}, {2, 'b'}, {3, 'c'}};
my_data_t arr2[]{{1, 'a'}, {2, 'b'}, {3, 'c'}};
// using structured bindings for readabiliey
for(const auto& [number, character] : arr)
{
// for speed, don't use std::endl though
std::cout << number <<", " << character << "\n";
}
// or using own datastructure
for(const auto& entry : arr2)
{
std::cout << entry.number <<", " << entry.character << "\n";
}
return 0;
}
They are the same. The template arguments 0 and 1 in get<> must be compile-time constants, An implementation of get<>() refers to first or second for 0 or 1 respectively. The function call to get<>() gets inlined when optimized.

Saving A Character that you pop out of a char vector [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Im trying to save a char that im going to pop out of the char vector can you do that? If so how?
You are saving the character. This code
char char_toInt = vect[vect.size() - 1];
saves the character perfectly well. Not sure why you thought that was incorrect.
The mistake is the next line
int new_int = stoi(char_toInt);
Instead do it like this
int new_int = char_toInt - '0';
stoi is for a string and you have a char. These are not the same thing, and C++ is fussy about types.
Instead to convert a digit char to it's integer value just subtract '0' from the char. All chars are represented as integers internally (lookup ASCII if you are interested in the how this might be done). Subtracting '0' gives you the difference between the integer corresponding to your digit and the integer corresponding to '0'. Since the integers corresponding to the digits are guaranteed to be sequential this gives you the value of your digit.
Well, you can write something like this
#include <vector>
#include <iostream>
int main()
{
std::vector<char> v;
v.push_back('a');
v.push_back('b');
v.push_back('c');
std::cout << v.back() << std::endl;
v.pop_back();
std::cout << v.back() << std::endl;
v.pop_back();
std::cout << v.back() << std::endl;
v.pop_back();
return 0;
}
which would produce an output of
c
b
a
Alternatively, you can also use a queue:
#include <queue>
#include <iostream>
int main()
{
std::queue<char> q;
q.push('a');
q.push('b');
q.push('c');
std::cout << q.front() << std::endl;
q.pop();
std::cout << q.front() << std::endl;
q.pop();
std::cout << q.front() << std::endl;
q.pop();
return 0;
}
which would produce an output of:
a
b
c
Or, alternatively, you can use a stack:
#include <stack>
#include <iostream>
int main()
{
std::stack<char> s;
s.push('a');
s.push('b');
s.push('c');
std::cout << s.top() << std::endl;
s.pop();
std::cout << s.top() << std::endl;
s.pop();
std::cout << s.top() << std::endl;
s.pop();
return 0;
}
which would produce an output of:
c
b
a

compare two vectors of string (character) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to compare two vectors string
vector <string> morse ={"A.-","B-...","C-.-.", "D-..", "E.", "F..-.", "G--.", "H....", "I.." ,"J.---", "K-.-", "L.-..", "M--" ,"N-." ,"O---" ,"P.--.", "Q--.-", "R.-.", "S...", "T-", "U..-", "V...-", "W.--" ,"X-..-" ,"Y-.--", "Z--.."};
vector<string> codeMorse (1);
codeMorse ={".---.--.-.-.-.---...-.---."};
if (morse[i][j]==codeMorse[k]){ //my problem here =error
}
Can anybody help me?
Your code has 2 problems:
You can't make 2-Dimensional vector that way nor you even tried to make it 2D.
You wrote morse[i][j] without previous definition of i and j.
To fix issue 1 & 2:
Include
#include <vector>
Make a vector of std::pair(s):
std::vector<std::pair<std::string, std::string>> morse;
That allows you to have a pair of strings.
To add a new morse code, use this:
morse.push_back(std::pair<std::string, std::string>("LETTER HERE", "MORSE CODE HERE"));
To read 'em use this:
//read all via loop
for (int i = 0; i <= morse.size(); i++) {
std::cout << "Letter: " << morse[i].first << std::endl; //.first access your first elemt of the pair
std::cout << "Morse Code: " << morse[i].second << std::endl; //.second to access the morse code
}
OR use iterators if you already know them:
//read all via loop
for (auto i = morse.begin(); i != morse.end(); i++) {
std::cout << "Letter: " << i->first << std::endl; //->first access your first elemt of the pair
std::cout << "Morse Code: " << i->second << std::endl; //->second to access the morse code
}
Of course you can read specific values:
std::cout << morse[0].first << std::endl; //[] same use as the array's brackets
std::cout << morse[0].second << std::endl; //same here

Remove an element from vector and change the size [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to remove elements from a vector and it works fine with the erase() methode, but after removing the element the size of the vector still the same.
std::vector<int> myvector;
myvector.push_back (1);
myvector.push_back (2);
myvector.push_back (3);//here the size is 3
myvector.erase(myvector.begin()+1);//I think normally the size should be 2 after removing the element
is there a function that can do that or should I do it manually, I'm new to c++ I checked the documentation and I didn't found a solution for this.
The size is changed then an element of a vector is removed with using member function erase. If you mean capacity then it will not be changed.
Here is a demonstrative program
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v { 1, 2, 3 };
std::cout << "v.size() = " << v.size() << std::endl;
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
v.erase( v.begin() + 1 );
std::cout << "v.size() = " << v.size() << std::endl;
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The output is
v.size() = 3
1 2 3
v.size() = 2
1 3