I have static arrays. How do I change them to dynamic ?

Pages: 12
table.clear() ; or table = {} ;
Thanks! Is there a *simple* way to print all table entries without passing reference numbers to the function?

Thank you
1
2
3
4
5
6
7
8
9
void print_all_entries( const lookup_table& table, std::ostream& stm = std::cout )
{
    for( const auto& entry : table ) // http://www.stroustrup.com/C++11FAQ.html#for
    {
        stm << "ref: " << entry.first << "  code_serial: [ " ;
        for( const auto& cs : entry.second ) stm << '{' << cs.code() << ',' << cs.serial() << "} " ;
        stm << "]\n" ;
    }
}

http://coliru.stacked-crooked.com/a/fd3835ee20b5f678
Last edited on
Thanks very much Mr JLBorges especially for the explanation of the special "range-for" statement, now I understand! Have a great day!
I'm now trying to figure out how I could sort & print each entry on cs.code() (which is a string). I have found this on the internet but it wouldn't work since it would only sort cs.code() and not its corresponding cs.serial(). Any clue? Thanks!

#include <string>
#include <vector>
#include <algorithm>

std::vector<std::string> stringarray;
std::sort(stringarray.begin(), stringarray.end());
Last edited on
We have inherited struct code_serial from std::pair< std::string, unsigned int >.
So we get the functionality of std::pair for free - that was the primary reason for inheriting from std::pair.

std::pair is LessThanComparable; ergo our struct code_serial is also LessThanComparable.

1
2
void sort_all_entries_on_code( lookup_table& table )
{ for( auto& entry : table ) std::sort( entry.second.begin(), entry.second.end() ) ; }

http://coliru.stacked-crooked.com/a/0ec9d5af7dd43ece
Thanks it works! You're the best!
Topic archived. No new replies allowed.
Pages: 12