How to insert element at beginning of vector - c++

vector<int>grid = { 0, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 3, 3, 3, 2, 2, 4, 1, 5, 3, 3, 6, 2, 6, 4, 5, 5, 5, 3, 6, 2, 6, 4, 4, 5, 5, 5, 6, 6, 6, 4, 7, 7, 8, 5, 8, 8, 8, 4, 7, 7, 8, 8, 8, 8, 8, 4, 7, 7, 7, 7, 8, 8, 8 };
const size_t gridSize = end(grid) - begin(grid);
int maxColour = *max_element(begin(grid), end(grid));
vector<vector<int>> colourPos(maxColour+1);
for (size_t i = 1; i < gridSize; ++i)
colourPos[grid[i]].push_back(i);
for (size_t i = 0; i < colourPos.size(); ++i) {
std::cout << (i + 1) << ": ";
for (int p : colourPos[i])
std::cout << p << ' ';
std::cout << std::endl;
}
How can I insert an element at colourPos[1][0] so that it shifts all elements, and also to the other vectors within the colourPos vector?
e.g [2][0], [3][0].
I tried
colourPos[1][0].insert(0);
and just got "expression must have class type"

insert takes an iterator to indicate where to insert. To insert at the beginning of colourPos[1]:
colourPos[1].insert(colourPos[1].begin(), 0);

This insertion works:
vector<int> &cp1 = colourPos[1]; // & means reference to the subarray
cp1.insert(cp1.begin(), 2); // insertion

Related

getting "Too Many Initializer Values" error in 2D array

void Data::Paramters()
{
for (int i = 0; i < I; i++)
{
mc[i] = new int[K];
for (int k = 0; k < K; k++)
{
mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };
}
}
}
getting "Too Many Initializer Values" error in starting of { 4, 2, 2, 1, 3 }
where I=5 and K=4
The initializer is for a 4x5 2D matrix, so you should just write:
enum { I = 4, K = 5 };
int mc[I][K] = { { 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 } };
The expression mc[i][k] has the type int. It is not an array.
So this assignment statement
mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };
does not make a sense.
If K is a constant expression then you can allocate and initialize the two-dimensional array the following way
int ( *mc )[K] = new int[I][K]
{
{ 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 }
};
Here is a demonstration program.
#include <iostream>
int main()
{
const size_t K = 5;
size_t I = 4;
int ( *mc )[K] = new int[I][K]
{
{ 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 }
};
for ( size_t i = 0; i < I; i++ )
{
for (const auto &item : mc[i])
{
std::cout << item << ' ';
}
std::cout << '\n';
}
}
The program output is
1 0 2 3 5
4 2 2 1 3
4 3 4 1 3
3 5 6 4 2

Runtime library error connected with vector

This is my code which task is to check how many times does the number appear in my array.
Error message tells me that vector subscript is out of range.
int main() {
vector<int> numbers(1,-1);
int x;
int z;
bool exit;
int digits[] = { 2, 4, 5, 3, 2, 5, 6, 3, 5, 7, 9, 2, 1, 2, 3, 4, 5, 6, 4, 3, 2, 6, 3, 4, 4, 1, 3, 7, 9, 5, 9, 2, 3, 1, 2, 3, 4, 5, 6, 2, 1, 2, 3, 4, 5, 3, 2, 7, 7, 7 };
for (int i = 0; i < (sizeof(digits) / sizeof(int)); i++) {
z = 0;
bool exit = 1;
for (int j = 0; j < (sizeof(digits) / sizeof(int)); j++) {
x = digits[i];
for (int y = 0; y < (sizeof(numbers) / sizeof(int)); y++) {
if (x == numbers[y]) {
bool exit = 0;
}
}
if (digits[j] == x && exit) {
z++;
}
}
if (exit) {
cout << "Liczba: " << x << " wystepuje: " << z << " razy" << endl;
numbers.push_back(x);
}
}
}
Visual studio tells me that my code is valid but then during compilation , cmd shows me the error message which tells me that Vector subscript out of range . I just can't figure it out how to fix it exactly , bcs i have not found that i would initialize the vector which would not exist .
At least this statement
for (int y = 0; y < (sizeof(numbers) / sizeof(int)); y++) {
does not make sense because the expression sizeof(numbers) / sizeof(int) does not yield the number of elements in the vector. Instead use
for (int y = 0; y < numbers.size(); y++) {
If I have correctly understood your task then instead of the container std::vector it is better to use either std::map or std::unordered_map.
For example
#include <iostream>
#include <map>
int main()
{
int digits[] =
{
2, 4, 5, 3, 2, 5, 6, 3, 5, 7,
9, 2, 1, 2, 3, 4, 5, 6, 4, 3,
2, 6, 3, 4, 4, 1, 3, 7, 9, 5,
9, 2, 3, 1, 2, 3, 4, 5, 6, 2,
1, 2, 3, 4, 5, 3, 2, 7, 7, 7
};
std::map<int, size_t> frequency;
for ( const auto &item : digits )
{
++frequency[item];
}
for ( const auto &p : frequency )
{
std::cout << p.first << ": " << p.second << '\n';
}
return 0;
}
The program output is
1: 4
2: 10
3: 10
4: 7
5: 7
6: 4
7: 5
9: 3

printing all increasing subsequence using recursion

I'm trying to print all increasing subsequence with following.
But it is not working accordingly.
Please explain what actually my code doing.
I'm stuck since last 1 week.
But unable to figure out.
#include <bits/stdc++.h>
using namespace std;
int temp[1000];
int ans = 1;
int cnt = 0;
void solve(int *arr, int n, int k)
{
if(k == n){
cnt++;
for(int j = 0; j < n; j++){
cout<<temp[j]<<" ";
}
cout<<endl;
return;
}
for(int i = k; i < n; ++i){
if(arr[k] <= arr[i]){
temp[k] = -2;
solve(arr, n, i+1);
temp[k] = 2;
}
}
}
int main()
{
int arr[] = {4, 1, 13, 7, 0, 2, 8, 11, 3};
//int arr[] = {-1, 1 ,2, 3, 4};
//int arr[] = {-1,1,2,3,4,11, 5,6, 2, 9};
memset(temp, -1, sizeof(temp));
solve(arr, 9, 0);
cout<<cnt<<endl;
return 0;
}
Output should be total number of enumerating increasing subsequence.
This prints all increasing subsequences:
void print(const vector<int> v) {
for (size_t i = 0; i < v.size() - 1; ++i)
cout << v[i] << ", ";
cout << v.back() << endl;
}
int solve(const vector<int>& v, size_t start, vector<int>& sequence) {
print(sequence);
int count = 1;
for (size_t i = start; i < v.size(); ++i) {
if (v[i] >= sequence.back()) {
sequence.push_back(v[i]);
count += solve(v, i + 1, sequence);
sequence.pop_back();
}
}
return count;
}
size_t solve(const vector<int>& v) {
int count = 0;
for (size_t i = 0; i < v.size(); ++i) {
vector<int> sequence{v[i]};
count += solve(v, i + 1, sequence);
}
return count;
}
Usage:
vector<int> v{4, 1, 13, 7, 0, 2, 8, 11, 3};
auto count = solve(v);
cout << "Count: " << count << endl;
Output:
4
4, 13
4, 7
4, 7, 8
4, 7, 8, 11
4, 7, 11
4, 8
4, 8, 11
4, 11
1
1, 13
1, 7
1, 7, 8
1, 7, 8, 11
1, 7, 11
1, 2
1, 2, 8
1, 2, 8, 11
1, 2, 11
1, 2, 3
1, 8
1, 8, 11
1, 11
1, 3
13
7
7, 8
7, 8, 11
7, 11
0
0, 2
0, 2, 8
0, 2, 8, 11
0, 2, 11
0, 2, 3
0, 8
0, 8, 11
0, 11
0, 3
2
2, 8
2, 8, 11
2, 11
2, 3
8
8, 11
11
3
Count: 48

Return back to top of Vector once end has been reached c++

I currently have an iterator that randomly increments through a vector, I want to be able to return back to the top of the vector once the end has been reached. I have a seed placed in my random generator so I have the same sequence. Obviously at the moment the vector just goes out of scope
int main()
{
vector<int> vectorTest = { 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20};
vector <int>::iterator it;
for (it = vectorTest.begin(); it != vectorTest.end(); it = it + rand() % 6)
{
cout << *it << endl;
}
}
This is a not a use case for iterator. I suggest using common indexing bounded by modulo of vector length.
int j = 0;
for (int i = 0; j < 20 ; i = (i + rand() % 6) % vectorTest.size(), j++) {
cout << vectorTest[i] << endl;
}
You could use ranges::view::cycle to repeat the vector elements indefinitely.
int main()
{
std::vector<int> vectorTest = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
auto repeating = vectorTest | ranges::view::cycle;
for (auto it = repeating.begin(); /* how to end? */; it = it + rand() % 6)
{
std::cout << *it << std::endl;
}
}

Order 2D Arrays based on a sum of each row in C++

The questions pretty much sums it up. Here's what I have so far:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int sum1;
int sum2;
int sum3;
int sum4;
int sum5;
int sum6;
int sum7;
int sum8;
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
for(int i = 0; i < 8; i++) {
}
}
I'm planning on assigning the sum of each value from sum1 to sum8 to a row, then order each row by its total sum and display it to the user.
However, I keep getting stuck and can't find any good documentation. Can anyone help me write an effective function I can loop through in my for loop to add each of the rows to a sum, then return the sum and average all the rows out? (this will likely have to be more than one function but...whatever.)
If you want the sum of each row displayed in ascending order, the following code will do that...
// #include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
int sums[8] = {0};
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
// calculate the sums
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
sums[i] += a[i][j];
}
}
sort(begin(sums), end(sums)); // sort the sums
copy(begin(sums), end(sums), ostream_iterator<int>(cout, " ")); // display the sums
return 0;
}
I see you are a cool programmer. You do not seek an easy way.
Of course you could use an array of sums but it seems it is too easy.
Well, then try the following
#include <iostream>
#include <numeric>
int main()
{
int sum1;
int sum2;
int sum3;
int sum4;
int sum5;
int sum6;
int sum7;
int sum8;
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
size_t i = 0;
for ( auto p : { &sum1, &sum2, &sum3, &sum4, &sum5, &sum6, &sum7, &sum8 } )
{
*p = std::accumulate( a[i], a[i] + 7, 0 );
++i;
}
for ( int x : { sum1, sum2, sum3, sum4, sum5, sum6, sum7, sum8 } ) std::cout << x << std::endl;
}
The output is
34
28
20
31
32
28
37
41
Having the sums you can find any average. I hope you will do this yourself.