From this discussion, I have the following code to check if an element exists in an array:
#include <iostream>
#include <vector>
template <typename T, std::size_t N>
bool IsIn(T value, const T(&values)[N])
{
for (const T& array_value : values)
{
if (value == array_value) return true;
}
return false;
}
int main() {
int arr1[] = { 10, 20, 30 };
bool ee1 = IsIn(10, arr1);
std::cout << "ee1 = " << (ee1?"true":"false") << "\n";
return 0;
}
I believe this code is good for array of fixed size (at compile time) only. If the array is dynamically created (the number of elements is not known at compile time), is there any way I can modify the code to accommodate it?
PS: I am aware of vector. However, I am just curious if there is any way to avoid it.
Don't use C-style arrays unless you absolutely need to. Use std::array instead. For dynamic arrays, use std::vector.
You can then use iterators to make your function generic. However, this function already exists, it's called std::find. You can try to implement your own, for learning purposes, or look up an example implementation here: cppreference | find
#include <algorithm>
#include <array>
#include <iostream>
#include <string>
#include <vector>
int main(){
std::array<int, 3> static_array{1, 2, 3};
std::vector<int> dynamic_array{3, 4, 5};
std::string str = "Hello World";
std::array<int, 3>::iterator stat_found;
if( (stat_found = std::find(static_array.begin(), static_array.end(), 3)) != static_array.end() ){
std::cout << "Found 3 in static_array at pos: " << stat_found - static_array.begin() << "\n";
}
std::vector<int>::iterator dyn_found;
if( (dyn_found = std::find(dynamic_array.begin(), dynamic_array.end(), 3)) != dynamic_array.end() ){
std::cout << "Found 3 in dynamic_array at pos: " << dyn_found - dynamic_array.begin() << "\n";
}
std::string::iterator str_found;
if( (str_found = std::find(str.begin(), str.end(), 'W')) != str.end() ){
std::cout << "Found W in string at pos: " << str_found - str.begin() << "\n";
}
}
Without changing the body of your method, you can accommodate practically any collection type by abstracting over the collection type as well, i.e.
template <typename T, typename Collection>
bool IsIn(T value, const Collection &values)
{
/* ... */
}
However, as inifnitezero noted, the standard way of doing this is actually with iterators, and many implementations already exist in the standard library for this.
For a dynamic array you have to pass in the size in some form or another.
template <typename T>
bool IsIn(T value, const T *arr, std::size_t size) { ... }
You already know about std::vector, which knows it's own size, so I will skip that. That is the way to handle dynamic arrays. But not the only way to pass them to a function.
You can use std::span, which can be used for fixed sized arrays, std::array, std::vector and any container with random access iterator (sequential iterator? not sure). It's probably the most flexible thing to use.
You can also use begin and end const iterators. But that involves a lot of typing unless you already have 2 iterators when you want to call it.
Personally I think std::span covers all the bases. You can even make a span from iterators.
Related
I have following C++ object
std::vector<std::vector<SomeClass>> someClassVectors(sizeOFOuter);
where I know the size of "outer" vector, but sizes of "inner" vectors varies. I need to copy the elements of this structure into 1D array like this:
SomeClass * someClassArray;
I have a solution where I use std::copy like this
int count = 0;
for (int i = 0; i < sizeOfOuter; i++)
{
std::copy(someClassVectors[i].begin(), someClassVectors[i].end(), &someClassArray[count]);
count += someClassVectors[i].size();
}
but the class includes large matrices which means I cannot have the "vectors" structure and 1D array allocated twice at the same time.
Any ideas?
Do you previously preallocate someClassArray to a given size? I'd suggest using 1D vector for getting rid of known problems with the plain array if possible.
what about something like this:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main() {
std::vector<std::vector<int>> someClassVectors {
{1,2,3},
{4,5,6},
{7,8,9}
};
std::vector<int> flat;
while (!someClassVectors.empty())
{
auto& last = someClassVectors.back();
std::move(std::rbegin(last), std::rend(last), std::back_inserter(flat));
someClassVectors.pop_back();
}
std::reverse(std::begin(flat), std::end(flat));
int * someClassArray = flat.data();
std::copy(someClassArray, someClassArray + flat.size(), std::ostream_iterator<int>(std::cout, " "));
}
The extra reverse operation doesn't have an effect on memory metrics - such an approach helps to avoid unneeded memory reallocations resulting from removing vector elements from beginning to end.
EDIT
Inspired by comments I changed copy to move semantics
Embrace Range-v3 (or whatever will be introduced in C++20) and write a solution in (almost) a single line:
auto flattenedRange = ranges::views::join(someClassVectors);
this gives you a range in flattenedRange, which you can loop over or copy somewhere else easily.
This is a possible use case:
#include <iostream>
#include <vector>
#include <range/v3/view/join.hpp>
int main()
{
std::vector<std::vector<int>> Ints2D = {
{1,2,3},
{4},
{5,6}
};
auto Ints1D = ranges::views::join(Ints2D);
// here, going from Ints1D to a C-style array is easy, and shown in the other answer already
for (auto const& Int : Ints1D) {
std::cout << Int << ' ';
}
std::cout << '\n';
// output is: 1 2 3 4 5 6
}
In case you want to get a true std::vector instead of a range, before writing it into a C-style array, you can include this other header
#include <range/v3/range/conversion.hpp>
and pipe join's output into a conversion function:
auto Ints1D = ranges::views::join(Ints2D) | ranges::to_vector;
// auto deduces std::vector<int>
In terms of standard and versions, it doesn't really require much. In this demo you can see that it compiles and runs just fine with
compiler GCC 7.3
library Range-v3 0.9.1
C++14 standard (option -std=c++14 to g++)
As regards the copies
ranges::views::join(Ints2D) is only creating a view on Ints2D, so no copy happens; if view doesn't make sense to you, you might want to give a look at Chapter 7 from Functional Programming in C++, which has a very clear explanation of ranges, with pictures and everything;¹
even assigning that output to a variable, auto Ints1D = ranges::views::join(Ints2D);, does not trigger a copy; Ints1D in this case is not a std::vector<int>, even though it behaves as one when we loop on it (behaves as a vector because it's a view on it);
converting it to a vector, e.g. via | ranges::to_vector, obviously triggers a copy, because you are no more requesting a view on a vector, but a true one;
passing the range to an algorithm which loops on its elements doesn't trigger a copy.
Here's an example code that you can try out:
// STL
#include <iostream>
#include <vector>
// Boost and Range-v3
#include <boost/range/algorithm/for_each.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/range/conversion.hpp>
struct A {
A() = default;
A(A const&) { std::cout << "copy ctor\n"; };
};
int main()
{
std::vector<std::vector<A>> Ints2D = {
{A{},A{}},
{A{},A{}}
};
using boost::range::for_each;
using ranges::to_vector;
using ranges::views::join;
std::cout << "no copy, because you're happy with the range\n";
auto Ints1Dview = join(Ints2D);
std::cout << "copy, because you want a true vector\n";
auto Ints1D = join(Ints2D) | to_vector;
std::cout << "copy, despite the refernce, because you need a true vector\n";
auto const& Ints1Dref = join(Ints2D) | to_vector;
std::cout << "no copy, because we movedd\n";
auto const& Ints1Dref_ = join(std::move(Ints2D)) | to_vector;
std::cout << "no copy\n";
for_each(join(Ints2D), [](auto const&){ std::cout << "hello\n"; });
}
¹ In an attempt to try giving a clue of what a range is, I would say that you can imagine it as a thing wrapping two iterators, one poiting to the end of the range, the other one pointing to the begin of the range, the latter being incrementable via operator++; this opearator will take care of the jumps in the correct way, for instance, after viewing the element 3 in Ints2D (which is in Ints2D[0][2]), operator++ will make the iterator jump to view the elment Ints[1][0].
I have a method to shuffle arrays, but it's not working and I don't know how to fix it now. How can I create a new array in main from the array returned by shuffleArray as I can't assign the shuffled array to a new array and it's giving me the same index for the elements?
using namespace std;
template <class T> T min (T array, T size){
int min=array[0];
for(int i=1;i<size;i++){
if(array[i]<min){min=array[i];}
}
return min;
}
template <class T> T indexOf (T array[], const int size, T value){
for(int i=0;i<size;i++){
if(array[i]==value){return value;}
}
return -1;
}
template <class T> T shuffleArray (T array[], T size){
T* Array2 = new T[size];
for(int i=0;i<size;i++){
Array2[i]=array[i];
}
random_shuffle(&Array2[0],&Array2[size]);
return *Array2;
}
int main(){
int a[]= {1,2,3,4,5};
int index = indexOf(a, 5, 3);
cout << endl << "The index is:" << shuffleArray(a, 5)<<endl;
cout << endl << "The index is:" << index<<endl;
return 0;
}
Short Answer: Use std container as std::array, they have appropriate copy constructors and save you some headache. By the way std::array is also not slower as a raw array (it is basically the raw array, but wrapped in a class and given some nice member functions to work with it).
Detailed Answer: I am not entirely sure, what you like to print to std::cout. But most likely it should be the position of 3 before and after shuffling. Then the code should look like this (to compile with std=c++11 for use of constexpr and auto):
#include <iostream>
#include <algorithm> // std::min, std::find and std::random_shuffle
#include <array> // use std::array instead of raw pointers
// using namespace std; Bad idea, as tadman points out in comment
// use std::min instead of
// template <class T> T min (T array, T size)
// use std::find instead of
// template <class T> T indexOf (T array[], const int size, T value){
// T is not the value_type of the array, but the array iteself. Works also for all other std containers with random access iterators
template<class T> T shuffleArray(T array) // array is copied here
{
// shuffle
std::random_shuffle(array.begin(), array.end());
// return copy of array
return array;
}
int main()
{
constexpr int numberToFind = 3; // constexpr is not necessary, but the variable seems not intented to change in this code
// use standard container instead of raw array
std::array<int,5> a = {1,2,3,4,5};
// iterator to numberToFind in a
auto it = std::find(a.begin(), a.end(), numberToFind); // auto deduces the type, so you do not have to write std::array<int,t>::iterator and by the way the code is more flexible for changes
// shuffle a and store result in aShuffled
auto aShuffled = shuffleArray(a);
// iterator to numberToFind in aShuffled
auto itShuffled = std::find(aShuffled.begin(), aShuffled.end(), numberToFind);
// pointer arithmetics give the index
std::cout << "The index before shuffling is:" << it - a.begin() << std::endl;
std::cout << "The index after shuffling is:" << itShuffled - aShuffled.begin() << std::endl;
return 0;
}
As some comments already tell you, a few tips for the future:
Use std containers instead of raw pointers of raw arrays
Use well tested algorithms from the standard library instead of writing your own, unless you have very specific needs
auto makes live very easy in C++11 to handle also the iterator types. By the way you have to change almost nothing to use std::vector instead of std::array. The first declaration is enough.
Always when you use a new there has to be a delete. Otherwise you create a memory leak. Again this can be generally avoided by using the standard container.
Use descriptive names instead of literals. This makes your code more clear and readable.
Is this undefined behavior?
std::array<int, 5> x = {3, 5, 1, 2, 3};
std::array<int, 3>& y = *reinterpret_cast<std::array<int, 3>*>(&x[1]);
for(int i = 0; i != 3; i++) {
std::cout << y[i] << "\n";
}
Maybe yes, but I really feel like there should be a safe way to slice std::arrays.
EDIT: Following Radek's suggestion:
template<unsigned N, unsigned start, unsigned end, typename T>
std::array<T, end - start>& array_slice(std::array<T, N>& x)
{
static_assert(start <= end, "start <= end");
static_assert(end <= N-1, "end <= N");
return *reinterpret_cast<std::array<T, end - start>*>(&x[start]);
}
EDIT: Ok, I decided that I'm unhappy with std::arrays and will move to something else, any ideas?
Yes, that is undefined behavior. You're taking one type and reinterpret_casting it to another. Indeed, the use of the reinterpret_cast should be a big red flag for "here there be dragons!"
As for slicing arrays, that's not going to happen. A std::array contains values; a slice of this would contain references to part of that array. And therefore, it would not be a std::array. You can copy slices of arrays, but not using std::array. You would need to use std::vector, since it allows the calling of constructors, as well as construction from a range of values. Remember: std::array is just a nicer wrapper around a C-style array.
The committee is looking into a template array_ref<T> class, which is exactly what it says: a reference to some segment of an array of type T. This could be a regular C-style array, a std::vector, a std::array, or just some memory allocated with new T[]. There are some library implementations of the class already, but nothing is standardized yet.
Following Radek's suggestion:
Hiding the undefined behavior in a function does not make it defined behavior. You can try to pretend that it isn't undefined, but it still is. The moment you use that reinterpret_cast, you willingly give up living in C++-land.
What about a placement new?
#include <array>
#include <iostream>
#include <iterator>
template<typename T, std::size_t N>
struct array_slice : public std::array<T,N> {
~array_slice() = delete;
};
int main() {
std::array<double,4> x_mu{0.,3.14,-1.,1.};
std:: cout << &x_mu << std::endl;
{
auto slicer = [] (std::array<double,4>& ref) {
array_slice<double,3>* p = new (&ref) array_slice<double,3>;
return p;
};
std::array<double,3>& x_ = *slicer(x_mu);
std::copy(x_.begin(),x_.end(),
std::ostream_iterator<float>(std::cout," "));
std:: cout << std::endl;
std:: cout << &x_ << std::endl;
}
std::copy(x_mu.begin(),x_mu.end(),
std::ostream_iterator<float>(std::cout," "));
}
Here is my code which attempts to search for a string of chars "gold" in the last four elements of a vector. It does successfully find the string but is this safe to do? It works on MS VS2008.
#include <vector>
#include <iostream>
int main() {
char random[] = {'a','b','c','d','e','f','g'};
char tofind2[] = {'g','o','l','d'};
std::vector<char> buf;
buf.insert(buf.end(), random, random+sizeof(random));
buf.insert(buf.end(), tofind2, tofind2+sizeof(tofind2));
if(buf.size() >= sizeof(tofind2) && std::equal(buf.end()-sizeof(tofind2), buf.end(), tofind2)) {
std::cout << "found value in last " << sizeof(tofind2) << " elements of array\n";
}
}
This is safe so long as your vector has at least 4 elements in it: iterators in general can be moved through the bounds of their range, and random access iterators can be moved via addition/subtraction of an integer type. std::vector's iterators are random access iterators.
If it has less than 4 elements, this is not safe, and leads to undefined behavior (even before you dereference the iterator!)
If you want to be careful, you should check for that case.
template<typename Container>
auto nth_last_iterator( Container&& c, int n )
-> declval( std::begin(c) )
{
if (n > std::end(c) - std::begin(c))
n = std::end(c) - std::begin(c);
return std::end(c)-n;
}
which is C++11 and works on any random access container. Then you get:
if(std::equal(nth_last_iterator(buf,sizeof(tofind2)), buf.end(), tofind2)) {
std::cout << "found value in last " << sizeof(tofind2) << " elements of array\n";
}
As noted by #DavidHammen, sizeof(tofind2) only works if sizeof(tofind2[0]) == 1. There are some relatively easy to write templates that find the size of an array and don't have that weakness, such as:
template<typename T, std::size_t N>
std::size_t lengthof( T(&)[N] ) {
return N;
}
which is valid C++03, and in C++11 you can make it constexpr. (and you can extend it to std::array< T, N > const& as well)
This is correct, you can safely do that since iterator arithmetic is allowed (http://www.cplusplus.com/reference/iterator/RandomAccessIterator/).
Is this undefined behavior?
std::array<int, 5> x = {3, 5, 1, 2, 3};
std::array<int, 3>& y = *reinterpret_cast<std::array<int, 3>*>(&x[1]);
for(int i = 0; i != 3; i++) {
std::cout << y[i] << "\n";
}
Maybe yes, but I really feel like there should be a safe way to slice std::arrays.
EDIT: Following Radek's suggestion:
template<unsigned N, unsigned start, unsigned end, typename T>
std::array<T, end - start>& array_slice(std::array<T, N>& x)
{
static_assert(start <= end, "start <= end");
static_assert(end <= N-1, "end <= N");
return *reinterpret_cast<std::array<T, end - start>*>(&x[start]);
}
EDIT: Ok, I decided that I'm unhappy with std::arrays and will move to something else, any ideas?
Yes, that is undefined behavior. You're taking one type and reinterpret_casting it to another. Indeed, the use of the reinterpret_cast should be a big red flag for "here there be dragons!"
As for slicing arrays, that's not going to happen. A std::array contains values; a slice of this would contain references to part of that array. And therefore, it would not be a std::array. You can copy slices of arrays, but not using std::array. You would need to use std::vector, since it allows the calling of constructors, as well as construction from a range of values. Remember: std::array is just a nicer wrapper around a C-style array.
The committee is looking into a template array_ref<T> class, which is exactly what it says: a reference to some segment of an array of type T. This could be a regular C-style array, a std::vector, a std::array, or just some memory allocated with new T[]. There are some library implementations of the class already, but nothing is standardized yet.
Following Radek's suggestion:
Hiding the undefined behavior in a function does not make it defined behavior. You can try to pretend that it isn't undefined, but it still is. The moment you use that reinterpret_cast, you willingly give up living in C++-land.
What about a placement new?
#include <array>
#include <iostream>
#include <iterator>
template<typename T, std::size_t N>
struct array_slice : public std::array<T,N> {
~array_slice() = delete;
};
int main() {
std::array<double,4> x_mu{0.,3.14,-1.,1.};
std:: cout << &x_mu << std::endl;
{
auto slicer = [] (std::array<double,4>& ref) {
array_slice<double,3>* p = new (&ref) array_slice<double,3>;
return p;
};
std::array<double,3>& x_ = *slicer(x_mu);
std::copy(x_.begin(),x_.end(),
std::ostream_iterator<float>(std::cout," "));
std:: cout << std::endl;
std:: cout << &x_ << std::endl;
}
std::copy(x_mu.begin(),x_mu.end(),
std::ostream_iterator<float>(std::cout," "));
}