With the code below, I cannot figure out why numbs[numbs.size()] doesn't give me an appropriate response. I would assume it would give me the last item in the sorted vector, in this case it should be the largest. Yet, cout << numbs[numbs.size()]spits out garbage, e.g.
Number 1 entered. [1], smallest: 1. there are 1 elements in the vector. 1.36617e-231 is the largest.
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main()
{
double number_input = 0.0;
string unit = " ";
vector<double> numbs;
while (cin >> number_input)
{
numbs.push_back(number_input);
cout << "Number " << number_input << "entered.\n";
for(int i = 0; i < numbs.size(); ++i)
{
cout << "[" << numbs[i] << "],";
}
sort(numbs.begin(),numbs.end());
cout << "smallest: " << numbs[0] << endl;
cout << "there are " << numbs.size() << " elements in the vector.\n";
cout << numbs[numbs.size()] << " is the largest.";
}
return 0;
}
Indexes in a vector are 0-based, just as arrays are. So the last value in a vector v is v[ v.size() - 1 ] assuming v.size() > 0
Related
In my std::list, i have 10 elements, and i want to move the number 1000 to the back of the list.
https://leetcode.com/playground/gucNuPit
is there a better way, a 1 liner using std::move, back inserter or any other C++ syntax to achieve this with consciously?
// Move the number 1000 to the end of the list
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main() {
list<int> myList({2,3,4,1000,5,6,7,8,9,10});
cout << "List before " << endl;
for(auto e : myList)
cout << e << " ";
// get iterator to the number 1000 in the list
list<int>::iterator findIter = std::find(myList.begin(), myList.end(), 1000);
int val_to_move_to_end = *findIter;
myList.erase(findIter);
myList.push_back(val_to_move_to_end);
cout << endl << endl << "List after " << endl;
for(auto e : myList)
cout << e << " ";
return 0;
}
You can use the std::list::splice(..) to achieve this
myList.splice(myList.end(), myList, std::find(myList.begin(), myList.end(), 1000));
Check out the documentation
I have to write a program to test an integer value to determine if it is odd or even, and make sure my output is clear and complete. In other words, I have to write the output like "the value 4 is an even integer". I was also hinted that I have to check the value using the remainder modulo.
The issue I have is with the scanf() function. I get a syntax error:
'%=' expected a ')'
How do I fix this?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
int scanf(%=2 , &number);
if (number == 0)
cout << "the value" << number << "is even";
else
cout << "the value" << number << "is odd";
return 0;
}
You are using scanf() incorrectly (read the scanf() documentation on cppreference.com). The first parameter expects a null-terminated string containing the format to scan, but you are not passing in anything that even resembles a string. What you are passing in is not valid string syntax, per the C++ language standard. That is why you are getting a syntax error.
You need to change this line:
int scanf(%=2 , &number);
To this instead:
scanf("%d", &number);
Though, in C++ you really should be using std::cin instead for input (you are already using std::cout for output):
std::cin >> number;
Try this:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
if (cin >> number)
{
if ((number % 2) == 0)
cout << "the value " << number << " is even";
else
cout << "the value " << number << " is odd";
}
else
cout << "the value is invalid";
return 0;
}
I know this question is a little dated, however, if you are able to use modern C++ features. You can write a constexpr helper function such as this:
#include <cstdint>
constexpr bool isEven(uint32_t value) {
return ((value%2) == 0);
}
Then in your main function, you can traverse through a loop of N integers and output your display such as:
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isEven(i) ? "even" : "odd") << '\n';
}
return 0;
}
It's literally that simple. Here's another nice feature of using the constexpr helper function... You can also format your output as such:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isEven(i) << '\n';
}
return true;
}
If you are looking for something that is more efficient than using the modulo operator you can bitwise & with the least significant digit... The code above would then become:
#include <cstdint>
constexpr bool isOdd(uint32_t value) {
return (value&1);
}
And using it would be very similar as above, just make sure you reverse the wording in your output to match that from the function being used...
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isOdd(i) ? "odd" : "even") << '\n';
}
return 0;
}
Again you can use the std::boolalpha manipulator to get this kind of output:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isOdd(i) << '\n';
}
return true;
}
I would like to see whether it is possible to see all values that we have emplaced. For example:
#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
unordered_multimap<string,int> hash;
hash.emplace("Hello", 12);
hash.emplace("World", 22);
hash.emplace("Wofh", 25);
for (int i = 1; i < 10; i++) {
hash.emplace("Wofh", i);
}
cout << "Hello " << hash.find("Hello")->second << endl;
cout << "Wofh " << hash.count("Wofh") << endl;
cout << "Wofh " << hash.find("Wofh")->second << endl;
return 0;
}
The output is :
$ ./stlhash
Hello 12
Wofh 10
Wofh 9
Whereas I want the last line to show from 25,1,2... to 9. Apparently find only takes first and second pointer as first is the value and second is the corresponding value. Is there any way to do this?
The operation you need is called equal_range
Example from the cplusplus.com:
// unordered_multimap::equal_range
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
typedef std::unordered_multimap<std::string,std::string> stringmap;
int main ()
{
stringmap myumm = {
{"orange","FL"},
{"strawberry","LA"},
{"strawberry","OK"},
{"pumpkin","NH"}
};
std::cout << "Entries with strawberry:";
auto range = myumm.equal_range("strawberry");
for_each (
range.first,
range.second,
[](stringmap::value_type& x){std::cout << " " << x.second;}
);
return 0;
}
I'm trying to add a string to the middle of a vector, but I don't want to lose the data that is being replaced. I want everything below that element to shift down one. Is that possible?
Here is what I have so far
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back("Rich");
cout << v.back() << endl;
v.push_back("Debbie");
cout << v.back() << endl;
v.push_back("Robin");
cout << v.back() << endl;
v.push_back("Dustin");
cout << v.back() << endl;
v.push_back("Philip");
cout << v.back() << endl;
v.push_back("Jane");
cout << v.back() << endl;
v.push_back("Joseph");
cout << v.back() << endl;
cout << "Removing Joseph from the vector"<<endl;
v.pop_back();
cout << "Adding my name to the vector" << endl;
vector<string>::iterator pos = v.find(v.begin(),v.end(), "Robin");
if (pos != v.end())
{
++pos;
}
v.insert(pos, "Jimmy");
cout << "The vector now contains the names:";
for (unsigned i=0; i<v.size(); i++)
cout << " " << "\n" << v.at(i);
cout << "\n";
return 0;
}
I'm also getting an error on this find function. Any help would be appreciated.
Error 1 error C2039: 'find' : is not a member of 'std::vector<_Ty>' d:\pf3\lab3b\lab3b\3b.cpp 28
2 IntelliSense: class "std::vector<std::string, std::allocator<std::string>>" has no member "find" d:\pf3\lab3b\lab3b\3b.cpp 28
Like this:
#include <vector> // for std::vector
#include <algorithm> // for std::find
v.insert(std::find(v.begin(), v.end(), "Robin"), "Jimmy");
std::vector has no find function, use std::find instead:
vector<string>::iterator pos = std::find(v.begin(),v.end(), "Robin");
this operation is O(N) in vector. if you will use it frequently please use linked list.
I want to print the first 2 values where the next is doubled from the current value.
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
bool doubled (int x, int y) { return x*2 == y; }
int main()
{
deque<int> di;
deque<int>::iterator diiter;
for (int i=0; i<=10; i+=2) di.insert(di.end(), i);
for (diiter = di.begin(); diiter != di.end(); ++diiter)
cout << *diiter << " ";
cout << endl;
diiter = adjacent_find(di.begin(), di.end(), doubled);
if (diiter != di.end()) {
cout << "found " << *diiter << " at " << distance(di.begin(), diiter)+1
<< " and " << *(++diiter) << " at " << distance(di.begin(), diiter)+1
<< endl;
}
}
the output is
0 2 4 6 8 10
found 4 at 3 and 4 at 2
not what I expected, which should be:
0 2 4 6 8 10
found 2 at 2 and 4 at 3
What's wrong with my code? I don't understand how the second position is decremented from the first one when I actually incremented it.
Thanks for all help.
Your program is giving strange results because it does not take in to account the fact, that order of evaluation of arguments to a function(In this case operator <<) is Unspecified.
My Answer here, explains the problem in detail & should be a good read.
You need to cout them on separate statements.
cout << "found " << *diiter;
cout << " at " << distance(di.begin(), diiter)+1;
cout << " and " << *(++diiter);
cout << " at " << distance(di.begin(), diiter)+1;
cout << endl;
This works well & outputs the correct/desired output.