completely removing a vector c++ - c++

I'm having problems removing a vector from a "multidimensional vector"
I would like to achieve this:
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2
3 3 3 3 4 4 4 4
4 4 4 4
for example
vector<vector<int>>vec;
for i...//give vec values...
vec[3].erase(vec.begin(),vec.end());
It seems like using vector.erase() or vector.clear() leaves an empty vector at the "third row"
Is there a way to completetly remove that vector so that
vec[3]=4 4 4 4
Thanx for a great forum...
/Bux

The following line removes the third element of vec. If it had four elements, it will have three after the line is executed.
vec.erase(vec.begin() + 2);
The following line, on the other hand, will leave the third vector empty.
vec[2].clear();

Related

Given that I have a list of vector <1, 2, 3, 4>, can next_permutation in algorithm library help me arrange 4P2 arrangement? In C++

can next_permutation avoid the duplication as what I want is to skip 2th and 4th as only change in first 2 character is important to me.
do {
//Do something
} while(next_permutation(s.begin(), s.end()));
this will get 4! = 24 solution, while I only wanted 4P2 = 12 solution.
The above coding will give me.
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
While actually I only want
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
do {
// Do something with the first two entries of `s`.
std::prev_permutation(s.begin()+2, s.end());
} while(std::next_permutation(s.begin(), s.end()));
This will essentially skip all permutations of the last two items, so it won't iterate all permutations unnecessarily and be relatively efficient even if you change the length of the vector or the number of items you are interested in (the magic number 2 in the code above).

Number of first element insertions to sort array in O(nlogn)?

If you am only allowed to move the first element of an array, how many insertions does it take to fully sort the array?
In the output, give the number of insertions necessary as well as how many positions each element moves back.
For example:
Input:
6
1 4 2 5 3 6
Output:
4
3 4 2 4
Explanation:
This is the order of insertions:
4 2 5 1 3 6
2 5 1 3 4 6
5 1 2 3 4 6
1 2 3 4 5 6
I can do this in O(n2) since the problem simplifies to finding the position where the first element lies in the increasing suffix of the array.
How can I solve this in O(nlogn)?

How to find and return a repeating sequence within a vector

I have a vector that is filled dynamically and will always contain a repeating sequence with characters and length that I am unsure of. For example, the vector could contain these elements:
0 1 1 2 3 1 0 1 1 2 3 1 0 1 1 2
and the repeating sequence in that vector is:
0 1 1 2 3 1
How can I search the vector and find those elements. I would like to put the found sequence in a new vector. I assumed at first it would only take a simple for loop and checking for repetition of the first and second element in the array, so in the case above I would exit the loop when I reached 0 1 a second time, but the problem is that it cannot be assumed that the first 2 elements will be in the repeating pattern, so
0 1 2 3 2 3 2 3 2 3
can be valid elements in the vector. Any ideas?
in general (infinite result) it is impossible to know the sequence because something like that can happen 1 million 0 and then 1,after 1000 zero u will think that the sequence is zero only,but if the vector is finite
you can write somethink like that
for(I..VECTORSIZE / 2)
if(VECTORSIZE % I == 0)
CHECK IF SUBVECTOR(0,I) == SUBVECTOR(I,I*2) == SUBVECTOR(I*2,I*3)....
return I
else continute;

SFrame: replacing of specific rows in the column

Sorry, I have probably some simple question.
I have SFrame looks like this:
A B C
0 1 2
0 2 3
1 2 3
1 3 4
2 3 1
2 3 3
. . .
Also I have another SFrame, looks like this:
A B C
0 1 4
0 2 5
I want replace SFrame with the similar A & B values, but with new C values.
A B C
0 1 4
0 2 5
1 2 3
1 3 4
2 3 1
2 3 3
. . .
It could be the all columns in the firstSFrame, but also just one column (SArray).
I try it with the next prompt:
sfr['C'][sfr['A']==0] = sfr2['C']
or just
sfr[sfr['A']==0] = sfr2
but got next error message:
TypeError: 'SArray' object does not support item assignment
Anyway, When I replace the SArray C from the similar length, this solution is worked.... The problem is in the different lengths of SFrames...
At the moment, I found someself a simple solution.
I create a list from all values, which I want replace in the first SFrame. Then convert this list to SArray and add it as a new column. (the number of columns is not important for me)...

Function that given these 2 values produces this third value?

I'm trying to write a function that when given 2 arguments, the 2 leftmost columns, produces the third column as a result:
0 0 0
1 0 3
2 0 2
3 0 1
0 1 1
1 1 0
2 1 3
3 1 2
0 2 2
1 2 1
2 2 0
3 2 3
0 3 3
1 3 2
2 3 1
3 3 0
I know there will be a modulus involved but I can't quite figure it out.
I'm trying to figure out if 4 people are sitting at a table, given the person and target, from the person's perspective which seat is the target sitting in?
Thanks
If a and b are the positions of the two persons, their "distance" is:
(4+b-a) % 4
This also shows that the forth block in your example is wrong.
Assuming that last block of numbers is wrong, I think you're looking for (4 + b - a) % 4 gives c (for columns a b c).