Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Array_values

 

STL

 

Array_values is an array example.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppArray_values/CppArray_values.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

include(../../Libraries/Boost.pri) #Or use the code below
# win32 {
#   INCLUDEPATH += \
#     ../../Libraries/boost_1_54_0
# }

SOURCES += main.cpp

 

 

 

 

 

./CppArray_values/main.cpp

 

#include <cassert>
#include <map>
#include <vector>

template <class KeyType, class ValueType>
const std::vector<ValueType> array_values(
  const std::map<KeyType,ValueType>& m)
{
  std::vector<ValueType> v;
  for (auto p: m)
  {
    v.push_back(p.second);
  }
  return v;
}

int main()
{
  {
    typedef int KeyType;
    typedef int ValueType;
    std::map<KeyType,ValueType> m;
    m[1] = 1;
    m[2] = 4;
    m[3] = 9;
    m[4] = 16;

    const std::vector<ValueType> values   { array_values(m) };
    const std::vector<ValueType> expected { 1,4,9,16 };
    assert(values == expected);
  }
  {
    typedef std::string KeyType;
    typedef int ValueType;
    std::map<KeyType,ValueType> m;
    m["four" ] = 4;
    m["one"  ] = 1;
    m["three"] = 3;
    m["two"  ] = 2;

    const std::vector<ValueType> values   { array_values(m) };
    const std::vector<ValueType> expected { 4,1,3,2 };
    assert(values == expected);
  }
  {
    typedef int KeyType;
    typedef std::string ValueType;
    std::map<KeyType,ValueType> m;
    m[1] = "one";
    m[2] = "two";
    m[4] = "four";
    m[8] = "eight";

    const std::vector<ValueType> values   { array_values(m) };
    const std::vector<ValueType> expected { "one", "two", "four", "eight" };
    assert(values == expected);
  }
  assert(1==2);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml