Related
This question already has answers here:
std::back_inserter for a std::set?
(2 answers)
Closed 12 months ago.
When I use transform on a set and use a vector to store the output, it works fine. But it doesn't seem to work the other way around.
This is the code that doesn't work:
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int multiply(int a) {
return a * 2;
}
void print(int i) {
cout << i << " ";
}
int main() {
int mynumbers[] = { 3,9,2,4,1 };
vector<int> v1(mynumbers, mynumbers + 5);
set<int> s1(mynumbers, mynumbers + 5);
transform(v1.begin(), v1.end(), s1.begin(), multiply);
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(s1.begin(), s1.end(), print);
}
As #molbdnilo pointed out:
The elements of a set are immutable.
Thus, existing elements cannot be overwritten.
However, it can be done with e.g. a std::insert_iterator:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5 };
std::set<int> s;
std::transform(v.begin(), v.end(),
std::insert_iterator<std::set<int>>(s, s.begin()),
[](int x) { return x * 2; });
for (int x : s) std::cout << ' ' << x;
}
Output:
2 4 6 8 10
Live demo on coliru
As #JeJo already mentioned, std::inserter can be used.
Just pasting here the code with it.
set<int> s1;
transform(v1.begin(), v1.end(), inserter(s1, s1.begin()), multiply);
For the testing purpose, it does not matter, although it is not good to initialize the set s1 with vector v1 values, because transform adds/overwrites entries in the target container, and in your code the new values are getting mixed with old values (e.g. 4,9)
I have a Sorted vector of strings and whenever a new string is added we have a function which will allow us to calculate the new position. I should be able to use this position and update the vector.
Example:
Input Vector v1{Acr,Adr,Apr,Arr,Asr};
Input New String : Aar
As soon as the String uis Added the Vector Becomes
v1{Acr,Adr,Apr,Arr,Asr,Aar};
After Addition new position is computed with a Function that Will Return position to insert I1 in this case).
So after Some operation it should become v1{Aar,Acr,Adr,Apr,Arr,Asr};
So you want to insert into an std::vector?
How about using std::vector::insert then?
Example:
v1.insert(v1.begin() + index, new_value);
As #Jarod42 commented, you could use std::lower_bound to find the insertion position, like this: C++ std::lower_bound() function to find insertion point for an index-sorted vector.
Sounds like a combination of std::vector::insert and std::lower_bound should be a good match:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
using std::vector;
using std::string;
using std::lower_bound;
using std::cout;
vector<string> v1{"Acr","Adr","Apr","Arr","Asr"};
auto insertionPoint{lower_bound(begin(v1), end(v1), "Aar")};
v1.insert(insertionPoint, "Aar");
for (auto&&s:v1) cout << s << " ";
cout << "\n";
return 0;
}
Demo
The key element is: v1.insert(insertionPoint, "Aar"); to insert directly in the right place. If you don't want to use lower_bound because you already have the index (let's call it pos for the sake of this example), you can always do sth like this v1.insert(begin(v1)+pos, "Aar");
And if you really need to append at the end initially (for whatever reason) using std::sort sounds like the most natural solution. Although the most inefficient (given you can modify the vector anyway).
Expanding the answer with custom comparison function:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class Sth
{
std::string name_;
public:
explicit Sth(const std::string& s) : name_(s) {}
const std::string& name() const {return name_;}
};
int main(int, char*[])
{
using std::vector;
using std::string;
using std::lower_bound;
using std::cout;
vector<Sth> v1{Sth("Acr"),Sth("Adr"),Sth("Apr"),Sth("Arr"),Sth("Asr")};
auto insertionPoint{lower_bound(begin(v1), end(v1), Sth("Aar"),
[](auto&& lhs, auto&& rhs){return lhs.name() < rhs.name();})};
v1.insert(insertionPoint, Sth("Aar"));
for (auto&&s:v1) cout << s.name() << " ";
cout << "\n";
return 0;
}
Demo
If I have understood correctly you mean something like the following
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<std::string> v = { "Acr", "Adr", "Apr", "Arr", "Asr" };
v.push_back( "Aar" );
auto pos = std::upper_bound( std::begin( v ), std::prev( std::end( v ) ),
v.back() );
if ( pos != std::prev( std::end( v ) ) )
{
auto s = std::move( v.back() );
v.pop_back();
v.insert( pos, s );
}
for ( const auto &s : v )
{
std::cout << s << ' ';
}
std::cout << '\n';
return 0;
}
The program output is
Aar Acr Adr Apr Arr Asr
I am trying to reorder the map in a descending way depending on the values, I have been trying to create a new map and insert the one which has the biggest value first but it keeps ordering the map by the keys.
I have also tried to reorder it by the value changing the form of the map into the other way but I will loose some data because I have more than one key which has the same value.
#include <iostream>
#include "SymbolFreq.h"
#include <string>
#include <fstream>
#include <streambuf>
#include <map>
using namespace std;
int main()
{
map <char, int> mymap;
map <char, int> realmap;
ifstream infile{ "ToCompress.txt" };
std::string str((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>());
std::map<char, int>::iterator itera;
for (auto it = str.begin(); it != str.end(); ++it)
{
itera = mymap.find(*it);
if (itera != mymap.end())
{
itera->second++;
}
else
{
mymap.insert({ *it, 1 });
}
}
int max = 0;
char provisionalChar;
int provisionalInt;
while (mymap.empty() == false)
{
for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
{
if (it->second > max)
{
max = it->second;
provisionalChar = it->first;
provisionalInt = it->second;
}
//cout << it->first << "\t" << it->second << "\n";
}
mymap.erase(provisionalChar);
realmap.insert({ provisionalChar, provisionalInt });
max = 0;
}
for (auto it = realmap.cbegin(); it != realmap.cend(); ++it)
{
cout << it->first << "\t" << it->second << "\n";
}
return 0;
}
If I understand the question properly, you'd like to count how many times each char appears in the file and then produce a map sorted with the char that appeared most time first.
Here's one idea:
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <unordered_map>
int main() {
std::ifstream infile{"ToCompress.txt"};
// "mymap" is only used for counting how many times each char appears.
std::unordered_map<char, std::uintmax_t> mymap;
// Loop directly over the file. No vector needed:
std::for_each(std::istreambuf_iterator<char>(infile),
std::istreambuf_iterator<char>(), [&mymap](char ch) {
// No need to find first - operator[] inserts an element
// for the key ("ch") if it's missing.
++mymap[ch];
});
// Transform the unordered_map into a multimap where the count is the key
// and in which we use a descending sort order (std::greater):
std::multimap<std::uintmax_t, char, std::greater<std::uintmax_t>> realmap;
std::transform(mymap.begin(), mymap.end(),
std::inserter(realmap, realmap.end()),
[](const auto& ch_co) -> std::pair<std::uintmax_t, char> {
// return a pair with key and value swapped
return {ch_co.second, ch_co.first};
});
// Print the result
for(auto& [count, ch] : realmap) {
std::cout << count << '\t' << ch << '\n';
}
}
Possible output:
537479
120204 t
113285 e
80681
80670 i
79862 n
77984 r
77464 s
69994 o
67377 a
...
Apparently, <space>, t, e and \n are tne most common characters in my C++ programs (which is what I used as input)
Your question may be ill-posed; take a step back and state what you are really trying to accomplish.
That said, I'll attempt an answer based on what you've written.
It looks like you're trying to sort an std::map by value, in which case your question is a duplicate of either this or this question.
Regarding your initial attempt:
Take a look at this table. Only sequence containers allow you to directly influence order. As with priority queue you have limited control over the order of associative containers and almost zero control for unordered containers.
In C++ STL, the map is used to map the key to a value. I want to know whether we can do this mapping based on some function say
map< int, string > M;
and value=binary_representation(key) ?
You can insert arbitrary (key, value) pairs into a std::map so you can certainly insert (xi, f(xi)) pairs for a function f and any x1, …, xn you like, provided that the types match.
The straight-forward way to do this might be to use a for loop.
for (const auto& k : keys)
mapping[k] = f(k);
Here is a complete example:
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
namespace /* anonymous */
{
std::string
my_function(const int n)
{
const auto size = static_cast<int>(CHAR_BIT * sizeof(int));
std::string bits {};
for (auto i = 0; i < size; ++i)
{
const auto bit = (n >> (size - i - 1)) & 1;
bits += (bit ? '1' : '0');
}
return bits;
}
}
int
main()
{
std::map<int, std::string> reprs {};
for (auto k = -3; k < 10; ++k)
reprs[k] = my_function(k);
for (const auto& kv : reprs)
std::cout << std::setw(4) << kv.first << " => " << kv.second << '\n';
}
Possible output:
-3 => 11111111111111111111111111111101
-2 => 11111111111111111111111111111110
-1 => 11111111111111111111111111111111
0 => 00000000000000000000000000000000
1 => 00000000000000000000000000000001
2 => 00000000000000000000000000000010
3 => 00000000000000000000000000000011
4 => 00000000000000000000000000000100
5 => 00000000000000000000000000000101
6 => 00000000000000000000000000000110
7 => 00000000000000000000000000000111
8 => 00000000000000000000000000001000
9 => 00000000000000000000000000001001
If you want to follow the advice of favoring algorithms over raw loops, you could use std::transform together with a std::insert_iterator to do the trick.
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <utility>
#include <vector>
namespace /* anonymous */
{
std::string
my_function(int); // as above
}
int
main()
{
std::vector<int> keys(13);
std::iota(keys.begin(), keys.end(), -3);
std::map<int, std::string> reprs {};
const auto generator = [](const int n){
return std::make_pair(n, my_function(n));
};
std::transform(keys.cbegin(), keys.cend(),
std::inserter(reprs, reprs.begin()),
generator);
for (const auto& kv : reprs)
std::cout << std::setw(4) << kv.first << " => " << kv.second << '\n';
}
However, I'm not sure if the use of iterators and algorithms really helps the readability of the code in this simple case. The use of the keys vector is a bit of an abomination here. If you have Boost, you could replace it by a boost::counting_iterator.
You could do, but it would be completely pointless. It would just be an inferior std::set<int> that doesn't properly guarantee the invariant and consumes a higher memory usage and runtime for absolutely no benefit whatsoever (unless you really wanted a cache).
Is there a way of printing arrays in C++?
I'm trying to make a function that reverses a user-input array and then prints it out. I tried Googling this problem and it seemed like C++ can't print arrays. That can't be true can it?
Just iterate over the elements. Like this:
for (int i = numElements - 1; i >= 0; i--)
cout << array[i];
Note: As Maxim Egorushkin pointed out, this could overflow. See his comment below for a better solution.
Use the STL
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <ranges>
int main()
{
std::vector<int> userInput;
// Read until end of input.
// Hit control D
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(userInput)
);
// ITs 2021 now move this up as probably the best way to do it.
// Range based for is now "probably" the best alternative C++20
// As we have all the range based extension being added to the language
for(auto const& value: userInput)
{
std::cout << value << ",";
}
std::cout << "\n";
// Print the array in reverse using the range based stuff
for(auto const& value: userInput | std::views::reverse)
{
std::cout << value << ",";
}
std::cout << "\n";
// Print in Normal order
std::copy(userInput.begin(),
userInput.end(),
std::ostream_iterator<int>(std::cout,",")
);
std::cout << "\n";
// Print in reverse order:
std::copy(userInput.rbegin(),
userInput.rend(),
std::ostream_iterator<int>(std::cout,",")
);
std::cout << "\n";
}
May I suggest using the fish bone operator?
for (auto x = std::end(a); x != std::begin(a); )
{
std::cout <<*--x<< ' ';
}
(Can you spot it?)
Besides the for-loop based solutions, you can also use an ostream_iterator<>. Here's an example that leverages the sample code in the (now retired) SGI STL reference:
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
short foo[] = { 1, 3, 5, 7 };
using namespace std;
copy(foo,
foo + sizeof(foo) / sizeof(foo[0]),
ostream_iterator<short>(cout, "\n"));
}
This generates the following:
./a.out
1
3
5
7
However, this may be overkill for your needs. A straight for-loop is probably all that you need, although litb's template sugar is quite nice, too.
Edit: Forgot the "printing in reverse" requirement. Here's one way to do it:
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
short foo[] = { 1, 3, 5, 7 };
using namespace std;
reverse_iterator<short *> begin(foo + sizeof(foo) / sizeof(foo[0]));
reverse_iterator<short *> end(foo);
copy(begin,
end,
ostream_iterator<short>(cout, "\n"));
}
and the output:
$ ./a.out
7
5
3
1
Edit: C++14 update that simplifies the above code snippets using array iterator functions like std::begin() and std::rbegin():
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
short foo[] = { 1, 3, 5, 7 };
// Generate array iterators using C++14 std::{r}begin()
// and std::{r}end().
// Forward
std::copy(std::begin(foo),
std::end(foo),
std::ostream_iterator<short>(std::cout, "\n"));
// Reverse
std::copy(std::rbegin(foo),
std::rend(foo),
std::ostream_iterator<short>(std::cout, "\n"));
}
There are declared arrays and arrays that are not declared, but otherwise created, particularly using new:
int *p = new int[3];
That array with 3 elements is created dynamically (and that 3 could have been calculated at runtime, too), and a pointer to it which has the size erased from its type is assigned to p. You cannot get the size anymore to print that array. A function that only receives the pointer to it can thus not print that array.
Printing declared arrays is easy. You can use sizeof to get their size and pass that size along to the function including a pointer to that array's elements. But you can also create a template that accepts the array, and deduces its size from its declared type:
template<typename Type, int Size>
void print(Type const(& array)[Size]) {
for(int i=0; i<Size; i++)
std::cout << array[i] << std::endl;
}
The problem with this is that it won't accept pointers (obviously). The easiest solution, I think, is to use std::vector. It is a dynamic, resizable "array" (with the semantics you would expect from a real one), which has a size member function:
void print(std::vector<int> const &v) {
std::vector<int>::size_type i;
for(i = 0; i<v.size(); i++)
std::cout << v[i] << std::endl;
}
You can, of course, also make this a template to accept vectors of other types.
Most of the libraries commonly used in C++ can't print arrays, per se. You'll have to loop through it manually and print out each value.
Printing arrays and dumping many different kinds of objects is a feature of higher level languages.
It certainly is! You'll have to loop through the array and print out each item individually.
This might help
//Printing The Array
for (int i = 0; i < n; i++)
{cout << numbers[i];}
n is the size of the array
std::string ss[] = { "qwerty", "asdfg", "zxcvb" };
for ( auto el : ss ) std::cout << el << '\n';
Works basically like foreach.
My simple answer is:
#include <iostream>
using namespace std;
int main()
{
int data[]{ 1, 2, 7 };
for (int i = sizeof(data) / sizeof(data[0])-1; i >= 0; i--) {
cout << data[i];
}
return 0;
}
You can use reverse iterators to print an array in reverse:
#include <iostream>
int main() {
int x[] = {1,2,3,4,5};
for (auto it = std::rbegin(x); it != std::rend(x); ++it)
std::cout << *it;
}
output
54321
If you already reversed the array, you can replace std::rbegin and std::rend with std::begin/std::end, respectively, to iterate the array in forward direction.
It's quite straightforward to copy the array's elements to a suitable output iterator. For example (using C++20 for the Ranges version):
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
template<typename T, std::size_t N>
std::ostream& print_array(std::ostream& os, std::array<T,N> const& arr)
{
std::ranges::copy(arr, std::ostream_iterator<T>(os, ", "));
return os;
}
Quick demo:
int main()
{
std::array example{ "zero", "one", "two", "three", };
print_array(std::cout, example) << '\n';
}
Of course it's more useful if we can output any kind of collection, not only arrays:
#include <algorithm>
#include <iterator>
#include <iosfwd>
#include <ranges>
template<std::ranges::input_range R>
std::ostream& print_array(std::ostream& os, R const& arr)
{
using T = std::ranges::range_value_t<R>;
std::ranges::copy(arr, std::ostream_iterator<T>(os, ", "));
return os;
}
The question mentions reversing the array for printing. That's easily achieved by using a view adapter:
print_array(std::cout, example | std::views::reverse) << '\n';
// Just do this, use a vector with this code and you're good lol -Daniel
#include <Windows.h>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<const char*> arry = { "Item 0","Item 1","Item 2","Item 3" ,"Item 4","Yay we at the end of the array"};
if (arry.size() != arry.size() || arry.empty()) {
printf("what happened to the array lol\n ");
system("PAUSE");
}
for (int i = 0; i < arry.size(); i++)
{
if (arry.max_size() == true) {
cout << "Max size of array reached!";
}
cout << "Array Value " << i << " = " << arry.at(i) << endl;
}
}
If you want to make a function that prints every single element in an array;
#include <iostream>
using namespace std;
int myArray[] = {1,2,3,4, 77, 88};
void coutArr(int *arr, int size){
for(int i=0; i<size/4; i++){
cout << arr[i] << endl;
}
}
int main(){
coutArr(myArray, sizeof(myArray));
}
The function above prints every single element in an array only, not commas etc.
You may be wondering "Why sizeoff(arr) divided by 4?". It's because cpp prints 4 if there's only a single element in an array.