Best way to split a vector into two smaller arrays? - c++

What I'm trying to do:
I am trying to split a vector into two separate arrays. The current int vector contains an element per line in a text file. The text file is a list of random integers.
How I'm planning to do it:
My current idea is to create two regular int arrays, then iterate over the entire vector and copy n/2 elements to each of the arrays.
What I would like to know:
What is the most elegant way of accomplishing my task? I have a feeling that I can do this without iterating over the vector multiple times.
Code:
#include <vector>
#include <fstream>
#include <iterator>
#include <iostream>
using namespace std;
vector<int> ifstream_lines(ifstream& fs)
{
vector<int> out;
int temp;
while(fs >> temp)
{
out.push_back(temp);
}
return out;
}
vector<int> MergeSort(vector<int>& lines)
{
int split = lines.size() / 2;
int arrayA[split];
int arrayB[split];
}
int main(void)
{
ifstream fs("textfile.txt");
vector<int> lines;
lines = ifstream_lines(fs);
return 0;
}
Thank you :)

Use iterators.
std::vector<int> lines;
// fill
std::size_t const half_size = lines.size() / 2;
std::vector<int> split_lo(lines.begin(), lines.begin() + half_size);
std::vector<int> split_hi(lines.begin() + half_size, lines.end());
Since iterator ranges represent half open ranges [begin, end), you don't need to add 1 to the second begin iterator: lines.begin() + half_size isn't copied to the first vector.
Note that things like
int split = lines.size() / 2;
int arrayA[split];
int arrayB[split];
Are not standard C++ (and as such not portable). These are so-called variable-length arrays (VLAs for short) and are a C99 thing. Some compilers have them as an extension while compiling C++ code (GCC, Clang). Always compile with -pedantic to get a warning. These VLAs act funky for non-POD types and aren't generally useful, since you can't even return them.

If you can't use code from Xeo answer due to strict compiler rules or you want more generic way, try std::advance:
#include <vector>
#include <iterator>
size_t middle = input.size()/2;
std::vector<int>::const_iterator middleIter(input.cbegin());
std::advance(middleIter, middle);
std::vector<int> leftHalf(input.begin(), middleIter);
std::vector<int> rightHalf(middleIter, input.end());

If you only need a reference to the numbers without manipulating them, then you can do:
int *array_1 = &lines[0];
int *array_2 = &lines[lines.size() / 2];
array_1 and array_2 are, actually, pointers to the start and middle of the vector. This works since STL guarantees that vectors store their elements within a continuous memory.
Note that referring to lines.begin() can't be used for this.

Solution to split vector to variable count parts using iterator.
#include <iostream>
#include <vector>
int main()
{
// Original vector of data
std::vector<double> mainVec{1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0};
// Result vectors
std::vector<std::vector<double>> subVecs{};
// Start iterator
auto itr = mainVec.begin();
// Variable to control size of non divided elements
unsigned fullSize = mainVec.size();
// To regulate count of parts
unsigned partsCount = 4U;
for(unsigned i = 0; i < partsCount; ++i)
{
// Variable controls the size of a part
auto partSize = fullSize / (partsCount - i);
fullSize -= partSize;
//
subVecs.emplace_back(std::vector<double>{itr, itr+partSize});
itr += partSize;
}
// Print out result
for (const auto& elemOuter : subVecs)
{
std::cout << std::fixed;
for (const auto& elemInner : elemOuter)
{
std::cout << elemInner << " ";
}
std::cout << "\n";
}
}

Related

Get number of same values in arrays in C++

I need a function int countDifferentNumbers(int v[], int n) which counts how many different values the array v with n entries contains.
Example:
It should return the result 3 for the array v = {1, 5, 5, 8, 1, 1} because the array contains only 3 different values.
This is how the code looks like so far:
int countDifferentNumbers(int v[], int n)
{
int counter = 0;
for(int i = 0; i < n; ++i)
{
for(int j = i; j < n; ++j)
{
if(v[i] == v[j + 1])
{
cout << "match" << endl;
counter++;
cout << v[i] << endl;
}
}
}
return counter;
}
I would appreciate an explanation of what is wrong in my function and how I need to redesign it.
Note: Unfortunately, I have not found a suitable thread for this either. All threads with my problems were solved in Java and Python languages.
Recently I see more and more answers here on SO that lead users in the wrong direction by giving bad answers.
Also, for C++, the question has already been answered in the comment by Igor Tandetnik, and that should finally be used.
But let me answer the question of the OP as asked. What is wrong with my function? OK, there are several aspects. Let us first look at the style.
You have 0 lines of comments, so the code quality is 0. If you would write comments, then you would already find most bugs by yourself, because then, you need to explain your own wrong statements.
Then please see your source code with my amendments. I added the problems as comment.
// This is just a dumped function and not a minimum reproducible example
// All header files are messing
// Obviously "using namespace std;" was used that should NEVER be done
// The function should retrun an unsigned value, best size_t, because a count can never be negative
// Same for n, that is the size of an array. Can also never be negative
// C-sytle arrays should NEVER be used in C++. NEVER. Use std::vector or std::array instead
int countDifferentNumbers(int v[], int n)
{
int counter = 0; // Now in C++ we can use braced initialzation instead of assignement
for (int i = 0; i < n; ++i)
{
for (int j = i; j < n; ++j)
{
if (v[i] == v[j + 1]) // Accessing out of bounds element
{
cout << "match" << endl; // Now endl needed here. Can all be done in one cout statement in one line
counter++; // Always counting up the same counter for all kind of double numbers.
cout << v[i] << endl;
}
}
}
return counter;
That was one point of the answer. But now the second point. Evene more important. The algorithm or the design is wrong. And finding the correct solution, this thinking before codingt, you need to do, before you write any line of code.
You obviously want to find the count of unique numbers in an array.
Then you could look what is already there on Stackoverflow. You would probaly find 20 answers already that coud give you a hint.
You could use std::unique. Please see here for a description. This function sounds like it does what you want, right? Some example implementation:
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
// If you want to keep the original data, remove the reference-specifier &
size_t countDifferentNumbers(std::vector<int>& v) {
std::sort(v.begin(), v.end()); // Sorting is precondition for std::unique
v.erase(std::unique(v.begin(), v.end()), v.end()); // Erase all non-unique elements
return v.size(); // Return the result
}
int main() {
std::vector test{ 1, 5, 5, 8, 1, 1 }; // Some test data
std::cout << countDifferentNumbers(test) << '\n'; // SHow result to user
return 0;
}
Then, we could count the occurence of each number in a std::map or std::unordered_map. And the number of counters will be the result. Example:
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
// If you want to keep the original data, remove the reference-specifier &
size_t countDifferentNumbers(std::vector<int>& v) {
std::unordered_map<int, size_t> counter{}; // Here we will count all occurences of different numbers
for (const int i : v) counter[i]++; // Iterate over vector and count different numbers
return counter.size(); // Count of different numbers
}
int main() {
std::vector test{ 1, 5, 5, 8, 1, 1 }; // Some test data
std::cout << countDifferentNumbers(test) << '\n'; // Show result to user
return 0;
}
But, then, thinking further, about what conatiners we could use, we will find out the answer from Igor Tandetnik. There are 2 containers that can hold unique values only. No double values. And these are: std::set and std::unordered_set., So, we can simply copy the data into one of those containers, and, only unique values will be stored there.
There are many ways to get the data into a set. But the simplest one is to use its range constructor. Then, we have unique elements, and, the containers size function will give the result:
See here: Constructor Number 2.
The result will be a function with one line like this
#include <iostream>
#include <unordered_set>
#include <vector>
// If you want to keep the original data, remove the reference-specifier &
size_t countDifferentNumbers(std::vector<int>& v) {
return std::unordered_set<int>(v.begin(), v.end()).size();
}
int main() {
std::vector test{ 1, 5, 5, 8, 1, 1 }; // Some test data
std::cout << countDifferentNumbers(test) << '\n'; // Show result to user
return 0;
}
And since functions with one line are often not so usefull, we can also write the final solution:
#include <iostream>
#include <unordered_set>
#include <vector>
int main() {
std::vector test{ 1, 5, 5, 8, 1, 1 }; // Some test data
std::cout << std::unordered_set<int>(test.begin(), test.end()).size() << '\n'; // Show result to user
return 0;
}
So, by analyzing the problem and choosing the right algorithm and container and using C++, we come to the most easy solution.
Please enable C++17 for your compiler.
first sort the array v. if n >0 then initially there must be one number which is unique so just increment the value of counter once. then with loop check if the two consecutive number are same or not. if same do nothing else increment the value of counter.
if you are writing code in c then use qsort. #include <stdlib.h> add this in header and. use qsort() func
here is the code:
#include <bits/stdc++.h>
using namespace std;
int countDifferentNumbers(int v[] , int n)
{
int counter = 0;
sort(v, v+ n); // if you are writing code in c then just write a decent sort algorithm.
if (n>0 ){
printf("%d\n", v[0]);
counter ++;
}
for(int i = 0; i < n-1; ++i)
{
if(v[i] == v[i+1]){
continue;
} else {
printf("%d\n", v[i+1]);
counter++;
}
}
return counter;
}
int main()
{
int v[] = {1, 5, 5, 8, 1, 1};
int result = countDifferentNumbers(v,6);
printf("unique number %d", result );
return 0;
}

Append data to array in C++

double b[3] = {3,3,4};
for(int i=0;i<3;i++) {
cout<<b[i]<<" "<<endl;
}
int sum = 0;
for(int i=0;i<3;i++) {
sum += b[i];
} cout<<"\n"<<sum<<endl;
for(int i=0;i<3;i++) {
double norm_b = b[i]/sum;
cout<<norm_b<<endl;
}
Result:
0.3
0.3
0.4
How to insert data (norm_b) to array, like append in Python?
The size of an array never changes through its lifetime. As such, there is no way to insert (nor to remove) elements.
What can be done instead, is to allocate an array dynamically, and upon insertion, allocate a bigger array, copy the elements from the old one, which can then be deallocated - or at least that is the general idea; I've skipped implementation details. Such data structure is called a resizable array. The standard library has an implementation of the resizable array: std::vector.
Because you mention Python, then there are better library classes and algorithms that are closer to what you might be used from Python.
#include <vector>
#include <algorithm>
#include <numeric>
void main() {
std::vector<double> b{3, 3, 4}; // Init vector
// If compiler supports C++11 you can use range-for
for (auto value : b)
{
std::cout << value << " " << std::endl;
}
double sum = std::accumulate(b.begin(), b.end(), 0); // sum b, sum should be double, not int?
std::cout << "\n"
<< sum << std::endl;
std::vector<double> norm_b(b.size()); // initialize norm_b with b.size() elements
size_t idx = 0;
std::generate(norm_b.begin(), norm_b.end(), [&b, &idx, &sum]() {
return b[idx++] / sum;
});
// Again range-for if C++11
for (auto value : norm_b)
{
std::cout << value << " " << std::endl;
}
}
There are a number of things in this code that you should look up if you don't fully understand:
Various way to initiliaze a std::vector (initializer list - braced initialization, count constructor etc)
std::vector iterators
std::accumulate
std::generate
lambdas
range-for
iterators based for (not used in the code above)
You can "append" a value only to a data structure that can dynamically grow in size. A c-like array if fixed size. You can try to use a std::vectorwhich has a push_back method, equivalent to python append method.
note: Instead of C-like array, consider to use std::array which is part of c++ STL and let you use STL algorithm on it.
Thank you guys
Solved
Update:
#include <vector>
vector<double> vec;
for(int i=0;i<3;i++) {
vec.push_back(b[i]/sum);
cout<<vec[i]<<endl;
}
you can use vector for append data as more as you like.For use vector you have to include a header file name vector.Here the code below:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//declare a vector first
vector<double>v;
int sum = 0;
for(int i=0;i<3;i++)
{
sum += b[i];
}
for(int i=0;i<3;i++)
{
vec.push_back(b[i]/sum);
cout<<vec[i]<<" "<<endl;
}
return 0;
}

While loop iterating on too many elements in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
When I run the code I get all those numbers as outputs which means that my while loop seems to be going over elements that it should not got over. Why is this happening ?
For context I am currently learning C++ with a tour of C++ and I am very confused with pointer and references.
This is where I got the code:
Buggy code in "A Tour of C++" or non-compliant compiler?
int counter(int* arr,int c){
int counter = 0;
while(*arr){
cout << *arr<<"\n";
if(*arr == c){
++counter;
}
++arr;
}
return counter;
}
int main()
{
int arr[3] = {1,2,3};
int count = counter(arr,1);
cout<< count;
}
Example run:
/Users/benediktschesch/CLionProjects/untitled/cmake-build-debug/untitled
1
2
3
-945684358
-1153026697
-280532248
32766
1839025881
32767
1839025881
32767
1
Process finished with exit code 0
This is very similar to not providing a null terminator for a character array that will be used as a string.
while(*arr)
means stop when you find a zero.
int arr[3] = {1,2,3};
provides no zero, therefore you have no control over when the loop will stop.
TL;DR Solution:
Use a Library container. std::vector or std::array would be a good fit here, as would std::count from the <algorithm> library and std::begin and std::end from the <iterator> library.
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
int arr[] = { 1, 2, 3 };
int count = std::count(std::begin(arr), std::end(arr), 1);
std:: cout << count;
}
Explanation:
You could provide a zero
int arr[] = {1,2,3,0};
Note I remove the explicit array size. It's not needed because the compiler knows from the number of elements in the initialzer.
Note also that this will stop upon reaching the first zero, so
int arr[] = {1,2,3,0,1,2,3,0};
will only spot one 1. This makes zero a very poor value to use to terminate a list of integers unless 0 is guaranteed to not be in the input.
To scan the whole array and only the array, the size of the array needs to be provided. This can be done by passing in a size parameter
int counter(int* arr, size_t len, int c)
{
int counter = 0;
while (len--)
{
std::cout << *arr << "\n";
if (*arr == c)
{
++counter;
}
++arr;
}
return counter;
}
int main()
{
int arr[3] = { 1, 2, 3 };
int count = counter(arr, std::size(arr), 1);
std:: cout << count;
}
but the preferred solution in Modern C++ would be to use a container in place of the array. Containers know their size and offer up a wide variety of other tools to make writing code easier and less error-prone.
#include <iostream>
#include <vector>
int counter(const std::vector<int> & arr, int c)
{
int counter = 0;
for (const auto & val: arr)
{
std::cout << val << "\n";
if (val == c)
{
++counter;
}
}
return counter;
}
int main()
{
std::vector<int> arr = { 1, 2, 3 };
int count = counter(arr, 1);
std:: cout << count;
}
Note the use of a range-based for loop to simplify the code. const auto & val deduces the type of val from the contents of arr with auto. The value is not going to be changed as a result of the loop so we declare it const to prevent accidents and make it a reference because maybe the compiler can pull off some extra optimization voodoo. In addition you can keep reusing this exact statement without having to change a thing if the container or the type of data in the container is ever changed. This prevents mistakes later when maintaining the code.
You could also use std::array and make counter a templated function that detects the size of the std::array here, but that's a bit much at this point.
The next evolution takes advantage of the <algorithm> library.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> arr = { 1, 2, 3 };
int count = std::count(arr.begin(), arr.end(), 1);
std:: cout << count;
}
In this case Iterators are being used rather than specifying lengths. This lets you easily scan subsets of a container.
This allows us to go back to using a vanilla array by taking advantage of std::begin and std::end to turn the array into a pair of iterators :
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
int arr[] = { 1, 2, 3 };
int count = std::count(std::begin(arr), std::end(arr), 1);
std:: cout << count;
}
And that brings us around to the TL;DR solution.
I recommend to use std::array and range based for loops
#include <array>
#include <iostream>
int counter(const std::array<int, 3> &arr, int c){
int counter = 0;
for (auto const a : arr) {
std::cout << a << "\n";
if(a == c){
++counter;
}
}
return counter;
}
int main()
{
std::array<int, 3> arr = {1,2,3};
int count = counter(arr,1);
std::cout << count;
}
The reason for your problem is that in the line
while(*arr){
the statement
*arr
is evaluated as boolean. It is false for *arr == 0 and true in any other case. In your code you need to get the size of the array or a last element with value 0. There are different ways. You can add a last element with 0 or you can pass the size to the function. But C++ standard provides stl containers that solve your problem without overhead. std::array is such a container that contains the data and the size of your array. It is a template class so that there is no extra data needed for the size. First you should learn how to use the tools that a language provides like stl containers and algorithms. Later you can learn how to use low level functions and data types.
Except character arrays that contain strings all other arrays (if you do not use intentionally a sentinel value) do not contain a zero element that signals the end of an array. So your function can invoke undefined behavior if the array does not contain an element equal to 0 (as in your case).
So in general your function should have one more parameter that specifies the number of elements in the array and should look like
size_t counter( const int *arr, size_t n, int value )
{
size_t count = 0;
for ( const int *p = arr; p != arr + n; ++p )
{
if ( *p == value ) ++count;
}
return count;
}
and called like
int main()
{
int arr[] = { 1, 2, 3 };
const size_t N = sizeof( arr ) / sizeof( *arr );
size_t count = counter( arr, N, 1 );
std::cout << count << '\n';
}
If your compiler supports C++ 17 then instead of the expression sizeof( arr ) / sizeof( *arr ) you can include standard function std::size() declared in the header <iterator> like std::size( arr ).
Otherwise instead of the expression sizeof( arr ) / sizeof( *arr ) you could use expression
std::extent<decltype( arr )>::value
provided that the header <type_traits> is included.
Take into account that there is standard algorithm std::count declared in header <algorithm> that performs the same task. Here is a demonstrative program
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
int arr[] = { 1, 2, 3 };
auto count = std::count( std::begin( arr ), std::end( arr ), 1 );
std::cout << count << '\n';
}

How to calculate sin and cos off all vector elements?

My code
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <sstream>
#include <cmath>
#define PI 3.14159265
int main(){
std::ifstream ifs("MFSO7.dat");
std::string line;
std::vector<float> column1;
std::vector<float> column2;
std::vector<float> column3;
std::vector<float> vkos;
std::vector<float> vsin;
while(std::getline(ifs, line)) // read one line from ifs
{
std::istringstream iss(line); // access line as a stream
float item1;
float item2;
float item3;
// Read the items from the line
iss >> item1 >> item2 >> item3;
// Add them to the columns.
column1.push_back(item1);
column2.push_back(item2);
column3.push_back(item3);
}
for(int i=0;i<38;i++)
{
vkos[i]=cos(column3[i]* PI/180.0 );
vsin[i]=sin(column3[i]* PI/180.0 );
}
std::cout << vkos[1] << std::endl;
}
Whem I execute the code I got
milenko#milenko-X58-USB3:~/Calibration Files$ ./a1
Segmentation fault (core dumped)
Why?May be I should avoid the loop or...?
A vector will have some capacity to hold new items. This is different from size, the count of elements that are actually in the vector. Thus a capacity of n that does not mean that it already has n items. A vector would have no items when it just got constructed by the default constructor -- the one with no arguments.
Referring to the ith element via vector::operator[] is incorrect when i >= n, where n is the size; in your case n is 0. So first you create them by vkos.push_back(cos(value)) instead of directly assigning to the index. On every push_back, the vector's size increases by one.
for(auto angle : column3)
{
vkos.push_back(cos(angle * PI/180.0));
vsin.push_back(sin(angle * PI/180.0));
}
if (vkos.size() >= 2)
cout << vkos[1] << '\n';
std::valarray is made for that, sin is overloaded with valarray:
vkos = cos(column3 * PI/180.0);
vsin = sin(column3 * PI/180.0);
No need for a loop, that would work.
If you insist on iterating with an index into a vector, you can use:
for (int i = 0; i < column3.size(); ++i) ...
This way, you will at least not try to access at an index that is larger than the current number or elements + 1.
Otherwise, you can try to initialize the vector to have exactly that many values:
std::vector<float> column3(38, 0);
Or if you are using C++11 you could even go for the
for (auto x : column3) ...
The problem is you are trying to assign to memory you don't have. Why don't you use std::transform? Using standard algorithms will help avoid these types of bugs, can often perform better than hand written loops, and most importantly is easier to read & understand. Also, avoid using #define, prefer constexpr.
constexpr double Pi {3.14159265};
constexpr auto PiDiv180 = Pi / 180;
std::transform(std::cbegin(column3), std::cend(column3), std::back_inserter(vkos),
[PiDiv180] (const auto v) { return std::cos(v * PiDiv180); });
std::transform(std::cbegin(column3), std::cend(column3), std::back_inserter(vsin),
[PiDiv180] (const auto v) { return std::sin(v * PiDiv180); });
It's because you're assigning to elements of vkos and vsin that haven't been constructed yet.
It's best to stick to the STL for these types of loop-based tasks, a lot of errors are taken care of for you. For this particular problem, you're looking for std::transform. The following code has zero overhead compared to your accepted solution:
std::vector<std::pair<float, float>> vkossin{};
std::transform(
std::begin(column3), std::end(column3), std::back_inserter(vkossin),
[](float degs) {
float rads = degs*PI/180.0;
return std::make_pair(cos(rads), sin(rads));
});
std::cout << vkossin[1].first << '\n';
std::back_inserter, std::pair, std::make_pair

How to iterate through a list of numbers in c++

How do I iterate through a list of numbers, and how many different ways are there to do it?
What I thought would work:
#include <cstdlib>
#include <iostream>
#include <list>
using namespace std;
int main()
{
int numbers[] = {2, 4, 6, 8};
int i = 0;
for(i=0; i< numbers.size();i++)
cout << "the current number is " << numbers[i];
system("pause");
return 0;
}
I get an error on the for loop line:
request for member 'size' in 'numbers', which is of non-class type 'int[4]'
Unlike a lot of modern languages plain C++ arrays don't have a .size() function. You have a number of options to iterate through a list depending on the storage type.
Some common options for storage include:
// used for fixed size storage. Requires #include <array>
std::array<type, size> collection;
// used for dynamic sized storage. Requires #include <vector>
std::vector<type> collection;
// Dynamic storage. In general: slower iteration, faster insert
// Requires #include <list>
std::list<type> collection;
// Old style C arrays
int myarray[size];
Your options for iteration will depend on the type you're using. If you're using a plain old C array you can either store the size somewhere else or calculate the size of the array based on the size of it's types. Calculating the size of an array has a number of drawbacks outlined in this answer by DevSolar
// Store the value as a constant
int oldschool[10];
for(int i = 0; i < 10; ++i) {
oldschool[i]; // Get
oldschool[i] = 5; // Set
}
// Calculate the size of the array
int size = sizeof(oldschool)/sizeof(int);
for(int i = 0; i < size; ++i) {
oldschool[i]; // Get
oldschool[i] = 5; // Set
}
If you're using any type that provides a .begin() and .end() function you can use those to get an iterator which is considered good style in C++ compared to index based iteration:
// Could also be an array, list, or anything with begin()/end()
std::vector<int> newschool;
// Regular iterator, non-C++11
for(std::vector<int>::iterator num = newschool.begin(); num != newschool.end(); ++num) {
int current = *num; // * gets the number out of the iterator
*num = 5; // Sets the number.
}
// Better syntax, use auto! automatically gets the right iterator type (C++11)
for(auto num = newschool.begin(); num != newschool.end(); ++num) {
int current = *num; // As above
*num = 5;
}
// std::for_each also available
std::for_each(newschool.begin(), newschool.end(), function_taking_int);
// std::for_each with lambdas (C++11)
std::for_each(newschool.begin(), newschool.end(), [](int i) {
// Just use i, can't modify though.
});
Vectors are also special because they are designed to be drop-in replacements for arrays. You can iterate over a vector exactly how you would over an array with a .size() function. However this is considered bad practice in C++ and you should prefer to use iterators where possible:
std::vector<int> badpractice;
for(int i = 0; i < badpractice.size(); ++i) {
badpractice[i]; // Get
badpractice[i] = 5; // Set
}
C++11 (the new standard) also brings the new and fancy range based for that should work on any type that provides a .begin() and .end(). However: Compiler support can vary for this feature. You can also use begin(type) and end(type) as an alternative.
std::array<int, 10> fancy;
for(int i : fancy) {
// Just use i, can't modify though.
}
// begin/end requires #include <iterator> also included in most container headers.
for(auto num = std::begin(fancy); num != std::end(fancy); ++num) {
int current = *num; // Get
*num = 131; // Set
}
std::begin also has another interesting property: it works on raw arrays. This means you can use the same iteration semantics between arrays and non-arrays (you should still prefer standard types over raw arrays):
int raw[10];
for(auto num = std::begin(raw); num != std::end(raw); ++num) {
int current = *num; // Get
*num = 131; // Set
}
You also need to be careful if you want to delete items from a collection while in a loop because calling container.erase() makes all existing iterators invalid:
std::vector<int> numbers;
for(auto num = numbers.begin(); num != numbers.end(); /* Intentionally empty */) {
...
if(someDeleteCondition) {
num = numbers.erase(num);
} else {
// No deletition, no problem
++num;
}
}
This list is far from comprehensive but as you can see there's a lot of ways of iterating over a collection. In general prefer iterators unless you have a good reason to do otherwise.
Change you for loop to
for(i=0; i< sizeof(numbers)/sizeof(int);i++){
In simple words,
sizeof(numbers) mean number of elements in your array * size of primitive type int, so you divide by sizeof(int) to get the number of elements
If you fix it so that it's list<int> numbers = {1,2,3,4}:
Iterating through using iterators:
#include <iterator>
for(auto it = std::begin(numbers); it != std::end(numbers); ++it) { ... }
Iterating through using std::for_each:
#include <algorithm>
#include <iterator>
std::for_each(numbers.begin(), numbers.end(), some_func);
Utilizing a for-each loop (C++11):
for(int i : numbers) { ... }
I didn't see it among the answers but this is imo the best way to do it: Range-based for loop
It is safe, and in fact, preferable in generic code, to use deduction to forwarding reference:
for (auto&& var : sequence).
Minimalist and working example :
#include <list>
#include <iostream>
int main()
{
std::list<int> numbers = {2, 4, 6, 8};
for (const int & num : numbers)
std::cout << num << " ";
std::cout << '\n';
return 0;
}
If your list of numbers is fixed be aware that you can simply write:
#include <iostream>
#include <initializer_list>
int main()
{
for (int i : {2, 4, 6, 8})
std::cout << i << std::endl;
return 0;
}
There is no size function on "plain" C-style arrays. You need to use std::vector if you want to use size, or calculate size through sizeof.
In C++11 you can use array initialization syntax to initialize your vectors, like this:
vector<int> numbers = {2, 4, 6, 8};
Everything else stays the same (see demo here).
You can also use the plain old C containers and use the iterator syntax for the loop:
#include <iostream>
int main()
{
int numbers[] = {2, 4, 6, 8};
int *numbers_end = numbers + sizeof(numbers)/sizeof(numbers[0]);
for (int *it = numbers; it != numbers_end; ++it)
std::cout << "the current number is " << *it << std::endl;
return 0;
}
There is no member function "size" because "numbers" isn't a class. You can not get the array's size this way, you must either know it (or compute it) or use some class to store your numbers.
The easiest way to do it, in my opinion, would be to use a span.
#include <cstdlib>
#include <iostream>
#include <gsl/span>
int main() {
int numbers[] = {2, 4, 6, 8};
for(auto& num : gsl::span(numbers)) {
cout << "the current number is " << num;
}
system("pause");
}
Notes:
Spans are part of the GSL library. To use them, download the library from here, and add the download path to the compilation command, e.g. g++ -o foo foo.cpp -I/path/to/gsl
In C++20, span will be part of the standard, so you would just use std::span and #include <span>.