So, I'm beginning C++, with a semi-adequate background of python. In python, you make a list/array like this:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Then, to print the list, with the square brackets included, all you do is:
print x
That would display this:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
How would I do the exact same thing in c++, print the brackets and the elements, in an elegant/clean fashion? NOTE I don't want just the elements of the array, I want the whole array, like this:
{1, 2, 3, 4, 5, 6, 7, 8, 9}
When I use this code to try to print the array, this happens:
input:
#include <iostream>
using namespace std;
int main()
{
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << anArray << endl;
}
The output is where in memory the array is stored in (I think this is so, correct me if I'm wrong):
0x28fedc
As a sidenote, I don't know how to create an array with many different data types, such as integers, strings, and so on, so if someone can enlighten me, that'd be great!
Thanks for answering my painstakingly obvious/noobish questions!
You can write a simple helper function to allow you to stream the array to an output stream (including but not limited to std::cout):
#include <iostream>
// print an array to an output stream
// prints to std::cout by default
template <typename T, std::size_t N>
void print_array(const T(&a)[N], std::ostream& o = std::cout)
{
o << "{";
for (std::size_t i = 0; i < N-1; ++i)
{
o << a[i] << ", ";
}
o << a[N-1] << "}\n";
}
where a function template is used in order to deduce both the type and size of the array at compile time. You can use it like this:
#include <fstream>
int main()
{
int a[] = {1,2,3,4,5};
print_array(a); // prints {1, 2, 3, 4, 5} to stdout
std::string sa[] = {"hello", "world"};
print_array(sa, std::cerr); // prints {hello, world} to stderr
std::ofstream output("array.txt");
print_array(a, output); // prints {1, 2, 3, 4, 5} to file array.txt
}
This solution can be trivially generalized to deal with ranges and standard library containers. For even more general approaches, see here.
As for the side note, you cannot do that in C++. An array can only hold objects of one type.
Inspired by the answers of juanchopanza and Raxman I decided to do a real IO manipulator, which leads to a syntax like:
const char* arr[] = { "hello", "bye" };
std::cout
<< "Woot, I can has " << print(arr)
<< " and even " << print(std::vector<int> { 1,2,3,42 }, ":") << "!\n";
printing
Woot, I can has { hello, bye } and even { 1:2:3:42 }!
Note
it works seamlessly with chained output streaming using operator<< as usual
it is fully generic (supporting any container of streamable types)
it even allows to pass a delimiter (as an example)
with a little more template arguments it could be made so generic as to work with ostream, wostream etc.
fun: Since the delimiter can be any streamable 'thing' as well, you could even... use an array as the delimiter:
std::cout << "or bizarrely: " << print(arr, print(arr)) << "\n";
resulting in the rather weird sample output:
or bizarrely: { hello{ hello, bye }bye }
Still demonstrates the power of hooking seamlessly into IO streams, if you ask me.
I believe it will not get much more seamless than this, in C++. Of course there is some implementing to do, but as you can see you can leverage full genericity, so you're at once done for any container of streamable types:
#include <iostream>
#include <vector>
namespace manips
{
template <typename Cont, typename Delim=const char*>
struct PrintManip {
PrintManip(Cont const& v, Delim d = ", ") : _v(v), _d(std::move(d)) { }
Cont const& _v;
Delim _d;
friend std::ostream& operator<<(std::ostream& os, PrintManip const& manip) {
using namespace std;
auto f = begin(manip._v), l(end(manip._v));
os << "{ ";
while (f != l)
if ((os << *f) && (++f != l))
os << manip._d;
return os << " }";
}
};
template <typename T, typename Delim=const char*>
manips::PrintManip<T, Delim> print(T const& deduce, Delim delim = ", ") {
return { deduce, std::move(delim) };
}
}
using manips::print;
int main()
{
const char* arr[] = { "hello", "bye" };
std::cout
<< "Woot, I can has " << print(arr)
<< " and even: " << print(std::vector<int> { 1,2,3,42 }, ':') << "!\n"
<< "or bizarrely: " << print(arr, print(arr)) << "\n";
}
See it live at http://ideone.com/E4G9Fp
for(int i=0;i<9;i++)
cout << anArray[i] << endl;
ahh ok with brackets it be such (simply array print logic for your arrays , u can make it more general in future)
cout<<'{';
for(int i=0;i<8;i++)
cout << anArray[i] <<',';
cout<<anArray[8]<<'}';
For python users and c++ lovers there is std::vector .
here how it be print logic for vector
//solution with [] operator
if(anVector.size()>=1){
std::cout<<"{";
for(int i=0;i<anVector.size()-1;i++){
std::cout<<anVector[i]<<',' ;
}
std::cout<<anVector[anVector.size()-1]<<'}' ;
}
//solution with iterator
std::vector<int>::iterator it =anVector.begin();
if(it!=anVector.end()){
std::cout << '{'<<*it;
++it;
for (; it != anVector.end(); ++it){
std::cout<<','<< *it ;
}
std::cout << '}';
}
Also check C++ 11 std::vector . In new standart initializing and other things more elegant
Probably the easiest way to get an array printed nicely (assuming it has a length greater than zero) is with something like:
std::cout << "{" << anArray[0];
for (int i = 1; i < sizeof (anArray) / sizeof (*anArray); i++)
std::cout << ", " << array[i];
std::cout << "}";
If you wish to be able to print less than the full array, you'll need a way to specify the length (which may be zero so you should handle that case:
if (length == 0)
std::cout << "{}";
else {
std::cout << "{" << anArray[0];
for (int i = 1; i < length; i++)
std::cout << ", " << array[i];
std::cout << "}";
}
There may be other variations of that such as printing at a given starting point rather than element zero but I won't go into that here. Suffice to say, it's just a small modification to the loop and if condition.
Of course, if you want to do it the simple way, with std::cout << myvariable;, you can consider wrapping the whole thing in a class and providing your own operator<< for it - that would be a more object-oriented way of doing things.
If you don't care too much about having the comma as a separator, you could also use output iterators.
#include <iostream>
#include <iterator>
#include <algorithm>
...
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "{ ";
std::copy(anArray, anArray + 9, std::ostream_iterator<int>(std::cout, " "));
std::cout << "}" << std::endl;
Another idea to achieve this, is to define a macro to convert c-arrays to std::vector and a template function to output a std::vector, like this:
#include <vector>
#include <iostream>
// convert an array (e.g. int32_t x[20]) into an std::vector
#define ARRAY_TO_STD_VECTOR(VAR, TYPE) std::vector<TYPE>(VAR, VAR + sizeof(VAR)/sizeof(TYPE))
// output of std::vector<T>
namespace std {
template<typename T>
std::ostream& operator<<(std::ostream& p, const std::vector<T>& v)
{
p << "{";
for (size_t i = 0; i < v.size(); ++i)
{
p << v[i];
if (i < (v.size() - 1)) p << ", ";
}
p << "}";
return p;
}
}
Having this, you can output your array like this:
int main()
{
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << ARRAY_TO_STD_VECTOR(anArray, int) << endl;
}
Related
I wrote this program that sorts numbers in vectors from greatest to least, it works great but the only thing that is bugging me is trying to remove the comma from the last number. Heres my code
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main() {
vector<int> vi1, vi2, vi3;
srand(987);
for (int i = 0; i < 10; ++i) vi1.push_back(rand() % 10);
sort(vi1.begin(), vi1.end());
for (int i = 0; i < 10; ++i) vi2.push_back(rand() % 10);
sort(vi2.begin(), vi2.end())
while(!vi1.empty() && !vi2.empty()) {
if(vi1.back()>=vi2.back()) {
vi3.push_back(vi1.back());
vi1.pop_back();
}
else {
vi3.push_back(vi2.back());
vi2.pop_back();
}
}
while(!vi1.empty()) {
vi3.push_back(vi1.back());
vi1.pop_back();
}
while(!vi2.empty()) {
vi3.push_back(vi2.back());
vi2.pop_back();
}
for (auto i = vi3.begin(); i != vi3.end(); ++i)
cout << *i << ", ";
cout << "\nBye..." << endl;
return 0;
}
and here is the output
9, 9, 9, 8, 8, 8, 6, 6, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0,
Bye...
as you can see after the last 0 there is a comma which doesn't make sense grammatically speaking. How can I remove that comma?
Rather than removing the simplest way is just to print commas only for other values than the first:
for (auto i = vi3.begin(); i != vi3.end(); ++i) {
if(i != vi3.begin()) {
cout << ", ";
}
cout << *i;
}
I am using this idiom regularly to format e.g. SQL parameter lists, or similar where a delimiter isn't wanted after the last element but for any others.
There are other ways to detect the first element (e.g. using a bool variable initialized to true before the loop starts, and set to false upon the first iteration).
For your example it seems just checking for vi3.begin() is the easiest and most natural way.
Here's the generic variant in pseudo code:
bool isFirstOutput = true;
for_each(element in list) {
if(not isFirstOutput) {
print delimiter;
}
print element;
isFirstOutput = false;
}
If begin, end are at least bidirectional iterators (as yours are),
you can just check initially that begin != end; if not print nothing; if so print
", "-puncuated elements incrementing begin != std::prev(end), and
finally print *begin(). like so:
main.cpp
#include <utility>
template<typename OStream, typename BiIter, typename Punct>
void punctuated(OStream && out, Punct && punct, BiIter begin, BiIter end)
{
if (begin != end) {
for (--end ;begin != end; ++begin) {
std::forward<OStream>(out) << *begin << std::forward<Punct>(punct);
}
std::forward<OStream>(out) << *begin;
}
}
#include <iostream>
#include <vector>
#include <list>
#include <set>
int main()
{
std::vector<int> v0;
std::vector<int> v1{{1}};
std::list<int> l{{1,2}};
std::set<int>s{{3,4,5,6,7}};
std::cout << "v0 [";
punctuated(std::cout,",",v0.begin(),v0.end());
std::cout << "]\n";
std::cout << "v1 [";
punctuated(std::cout,",",v1.begin(),v1.end());
std::cout << "]\n";
std::cout << "l [";
punctuated(std::cout,",",l.begin(),l.end());
std::cout << "]\n";
std::cout << "s [";
punctuated(std::cout,"|",s.begin(),s.end());
std::cout << "]\n";
return 0;
}
Output:
g++ -Wall -Wextra main.cpp && ./a.out
v0 []
v1 [1]
l [1,2]
s [3|4|5|6|7]
Live
I am working with a vector in Codefights for the "almostIncreasingSequence" challenge. Is there a way to track how many "steps" a sort method takes when so I can set a simple counter/flag to check against if the sort passes a predefined threshold?
Yes there is. You can use a custom comparison function with std::sort as follows:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> myVector { 2, 8, 5, 9, 3, 7, 1, 4, 6, 0 };
int counter = 0;
std::sort(myVector.begin(), myVector.end(), [&counter](int lhs, int rhs) {
counter++;
return lhs < rhs;
});
std::cout << "Steps: " << counter << std::endl;
for(auto e : myVector)
std::cout << e << ' ';
std::cout << std::endl;
return 0;
}
I'm not sure if it's possible since I didn't really find anything, but I have a linear array called "Grades" and I'm trying to output it all in one line.
int Grades[5] = { 3, 2, 5, 2 };
cout << "Grades:" << Grades << "\n";
I know the above doesn't work, but I want something like:
Grades: 3, 2, 5, 2
(minus the formatting/commas of course)
I know you can loop through it, but it just ends up printing it on a new line which I don't want.
You can cout a container on a single line by using std::ostream_iterator and std::copy. Something like this will do the task you need:
#include <iostream> // std::cout
#include <iterator> // std::ostream_iterator
#include <vector> // std::vector
#include <algorithm> // std::copy
int main () {
int Grades[5] = { 3, 2, 5, 2 };
std::copy ( Grades, Grades + 4, std::ostream_iterator<int>(std::cout, ", ") );
return 0;
}
This is the example from std::ostream_iterator's documentation adapted a bit to fit the particular example. Also see the result on ideone.
(Contributed by Rerito) If you are using c++11 or later you can also make use of std::begin and std::end from <iterator> to make the code cleaner:
#include <iostream> // std::cout
#include <iterator> // std::ostream_iterator
#include <vector> // std::vector
#include <algorithm> // std::copy
#include <iterator>
int main () {
int Grades[5] = { 3, 2, 5, 2 };
std::copy (std::begin(Grades), std::end(Grades), std::ostream_iterator<int>(std::cout, ", ") );
return 0;
}
And the result on ideone.
int Grades[5] = { 3, 2, 5, 2 };
cout << "Grades: ";
for (int i = 0; i < sizeof(Grades)/sizeof(int); i++) {
cout << Grades[i] << ", "; //minus the commas, remove (<< ", ") or to space out the grades, just remove the comma
}
Or
Based on juanchopanza's suggestion, you can do it this way;
int Grades[] = { 3, 2, 5, 2 };
cout << "Grades: ";
for (auto g : Grades) {
cout << g << ", "; //minus the commas, remove (<< ", ") or to space out the grades, just remove the comma
}
If you have latest compiler supporting C++11/C++14 then You can use range-based for loop,
#include <iostream>
using namespace std;
int main()
{
int Grades[5] = { 3, 2, 5, 2, 1 };
bool bFirst = true;
for (int & i : Grades)
{
std::cout << (bFirst ? "Grades: " : ", ") << i;
bFirst = false;
}
return 0;
}
It shows output like,
Grades: 3, 2, 5, 2, 1
You can define an overload for operator<< that takes a pointer to array:
#include <iostream>
template <typename T, int i>
std::ostream& operator<<(std::ostream& os, T (*arr)[i] )
{
for (auto e: *arr)
os << e << ",";
os << std::endl;
return os;
}
int main()
{
int Grades[5] = {2,34,4,5,6};
std::cout<<& Grades;
}
I want to remove last 5 elements from a std::map.
One way is:
for(int i=0; i < 5; i++)
{
map<string, LocationStruct>::iterator it = myLocations.end();
it--;
myLocations.erase(it);
}
Is there a good way to do that without looping?
Thanks,
a compilable demo of one way to do it. Note that because map iterators are not random access iterators, there's likely to be a loop involved under the covers during the call to std::prev() anyway.
#include <iostream>
#include <map>
#include <string>
using namespace std::string_literals;
std::map<int, std::string> m = {
{ 0, "zero"s },
{ 1, "one"s },
{ 2, "two"s },
{ 3, "three"s },
{ 4, "four"s },
{ 5, "five"s }
};
auto main() -> int
{
for (const auto& entry : m) {
std::cout << entry.first << ", " << entry.second << std::endl;
}
// erase operation here
m.erase(std::prev(m.end(), 5), m.end());
std::cout << "\nafter erase \n\n";
for (const auto& entry : m) {
std::cout << entry.first << ", " << entry.second << std::endl;
}
return 0;
}
expected output:
0, zero
1, one
2, two
3, three
4, four
5, five
after erase
0, zero
You can use the std::prev function to do navigation for you:
m.erase(prev(m.end(), 5), m.end());
Use range erase
auto i = m.begin();
std::advance(i, (m.size() - 5) );
m.erase( i, m.end() );
As far as I know, std::map has normal access iterator, so you have to traverse to last element every time to erase it from memory.
So, I'm beginning C++, with a semi-adequate background of python. In python, you make a list/array like this:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Then, to print the list, with the square brackets included, all you do is:
print x
That would display this:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
How would I do the exact same thing in c++, print the brackets and the elements, in an elegant/clean fashion? NOTE I don't want just the elements of the array, I want the whole array, like this:
{1, 2, 3, 4, 5, 6, 7, 8, 9}
When I use this code to try to print the array, this happens:
input:
#include <iostream>
using namespace std;
int main()
{
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << anArray << endl;
}
The output is where in memory the array is stored in (I think this is so, correct me if I'm wrong):
0x28fedc
As a sidenote, I don't know how to create an array with many different data types, such as integers, strings, and so on, so if someone can enlighten me, that'd be great!
Thanks for answering my painstakingly obvious/noobish questions!
You can write a simple helper function to allow you to stream the array to an output stream (including but not limited to std::cout):
#include <iostream>
// print an array to an output stream
// prints to std::cout by default
template <typename T, std::size_t N>
void print_array(const T(&a)[N], std::ostream& o = std::cout)
{
o << "{";
for (std::size_t i = 0; i < N-1; ++i)
{
o << a[i] << ", ";
}
o << a[N-1] << "}\n";
}
where a function template is used in order to deduce both the type and size of the array at compile time. You can use it like this:
#include <fstream>
int main()
{
int a[] = {1,2,3,4,5};
print_array(a); // prints {1, 2, 3, 4, 5} to stdout
std::string sa[] = {"hello", "world"};
print_array(sa, std::cerr); // prints {hello, world} to stderr
std::ofstream output("array.txt");
print_array(a, output); // prints {1, 2, 3, 4, 5} to file array.txt
}
This solution can be trivially generalized to deal with ranges and standard library containers. For even more general approaches, see here.
As for the side note, you cannot do that in C++. An array can only hold objects of one type.
Inspired by the answers of juanchopanza and Raxman I decided to do a real IO manipulator, which leads to a syntax like:
const char* arr[] = { "hello", "bye" };
std::cout
<< "Woot, I can has " << print(arr)
<< " and even " << print(std::vector<int> { 1,2,3,42 }, ":") << "!\n";
printing
Woot, I can has { hello, bye } and even { 1:2:3:42 }!
Note
it works seamlessly with chained output streaming using operator<< as usual
it is fully generic (supporting any container of streamable types)
it even allows to pass a delimiter (as an example)
with a little more template arguments it could be made so generic as to work with ostream, wostream etc.
fun: Since the delimiter can be any streamable 'thing' as well, you could even... use an array as the delimiter:
std::cout << "or bizarrely: " << print(arr, print(arr)) << "\n";
resulting in the rather weird sample output:
or bizarrely: { hello{ hello, bye }bye }
Still demonstrates the power of hooking seamlessly into IO streams, if you ask me.
I believe it will not get much more seamless than this, in C++. Of course there is some implementing to do, but as you can see you can leverage full genericity, so you're at once done for any container of streamable types:
#include <iostream>
#include <vector>
namespace manips
{
template <typename Cont, typename Delim=const char*>
struct PrintManip {
PrintManip(Cont const& v, Delim d = ", ") : _v(v), _d(std::move(d)) { }
Cont const& _v;
Delim _d;
friend std::ostream& operator<<(std::ostream& os, PrintManip const& manip) {
using namespace std;
auto f = begin(manip._v), l(end(manip._v));
os << "{ ";
while (f != l)
if ((os << *f) && (++f != l))
os << manip._d;
return os << " }";
}
};
template <typename T, typename Delim=const char*>
manips::PrintManip<T, Delim> print(T const& deduce, Delim delim = ", ") {
return { deduce, std::move(delim) };
}
}
using manips::print;
int main()
{
const char* arr[] = { "hello", "bye" };
std::cout
<< "Woot, I can has " << print(arr)
<< " and even: " << print(std::vector<int> { 1,2,3,42 }, ':') << "!\n"
<< "or bizarrely: " << print(arr, print(arr)) << "\n";
}
See it live at http://ideone.com/E4G9Fp
for(int i=0;i<9;i++)
cout << anArray[i] << endl;
ahh ok with brackets it be such (simply array print logic for your arrays , u can make it more general in future)
cout<<'{';
for(int i=0;i<8;i++)
cout << anArray[i] <<',';
cout<<anArray[8]<<'}';
For python users and c++ lovers there is std::vector .
here how it be print logic for vector
//solution with [] operator
if(anVector.size()>=1){
std::cout<<"{";
for(int i=0;i<anVector.size()-1;i++){
std::cout<<anVector[i]<<',' ;
}
std::cout<<anVector[anVector.size()-1]<<'}' ;
}
//solution with iterator
std::vector<int>::iterator it =anVector.begin();
if(it!=anVector.end()){
std::cout << '{'<<*it;
++it;
for (; it != anVector.end(); ++it){
std::cout<<','<< *it ;
}
std::cout << '}';
}
Also check C++ 11 std::vector . In new standart initializing and other things more elegant
Probably the easiest way to get an array printed nicely (assuming it has a length greater than zero) is with something like:
std::cout << "{" << anArray[0];
for (int i = 1; i < sizeof (anArray) / sizeof (*anArray); i++)
std::cout << ", " << array[i];
std::cout << "}";
If you wish to be able to print less than the full array, you'll need a way to specify the length (which may be zero so you should handle that case:
if (length == 0)
std::cout << "{}";
else {
std::cout << "{" << anArray[0];
for (int i = 1; i < length; i++)
std::cout << ", " << array[i];
std::cout << "}";
}
There may be other variations of that such as printing at a given starting point rather than element zero but I won't go into that here. Suffice to say, it's just a small modification to the loop and if condition.
Of course, if you want to do it the simple way, with std::cout << myvariable;, you can consider wrapping the whole thing in a class and providing your own operator<< for it - that would be a more object-oriented way of doing things.
If you don't care too much about having the comma as a separator, you could also use output iterators.
#include <iostream>
#include <iterator>
#include <algorithm>
...
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "{ ";
std::copy(anArray, anArray + 9, std::ostream_iterator<int>(std::cout, " "));
std::cout << "}" << std::endl;
Another idea to achieve this, is to define a macro to convert c-arrays to std::vector and a template function to output a std::vector, like this:
#include <vector>
#include <iostream>
// convert an array (e.g. int32_t x[20]) into an std::vector
#define ARRAY_TO_STD_VECTOR(VAR, TYPE) std::vector<TYPE>(VAR, VAR + sizeof(VAR)/sizeof(TYPE))
// output of std::vector<T>
namespace std {
template<typename T>
std::ostream& operator<<(std::ostream& p, const std::vector<T>& v)
{
p << "{";
for (size_t i = 0; i < v.size(); ++i)
{
p << v[i];
if (i < (v.size() - 1)) p << ", ";
}
p << "}";
return p;
}
}
Having this, you can output your array like this:
int main()
{
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << ARRAY_TO_STD_VECTOR(anArray, int) << endl;
}