Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an array, let's say 200 members, and then a smaller array, let's say 100 members. And if the larger array has 113 members, you can't fit the whole thing into the smaller array. So I want to have it so that it starts from the bottom up and tries to fit as much as it can into the smaller array. How do I do this?
int uniq() { static int current = 0; return ++current; }
int main()
{
int larger_array[200];
int smaller_array[100];
std::generate_n(larger_array, 113, uniq);
std::reverse_copy(std::begin(larger_array), std::end(larger_array), std::begin(smaller_array));
for (int i = 0; i < 100; ++i)
std::cout << smaller_array[i] << "\n";
return 0;
}
Use std::copy, something like:
std::copy(std::begin(largerArray), std::begin(largerArray) +
(std::end(smallerArray) - std::begin(smallerArray)),
std::begin(smallerArray));
If you know you want the last 100 elements of larger_array copied in reverse order this should work. The only difference from your code is using std::prev() to get the first iterator.
std::reverse_copy(std::prev(std::end(larger_array), 100), std::end(larger_array), std::begin(smaller_array));
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I was looking at a solution for this problem:
Given a string s, find the length of the longest substring without repeating characters.
The following solution was posted, but I am having trouble understanding what dict does. I've tried looking for documentation in C++. However, I have not found anything. Can someone explain how it works, and where I can find documentation?
int lengthOfLongestSubstring(string s) {
vector<int> dict(256, -1);
int maxLen = 0, start = -1;
for (int i = 0; i != s.length(); i++) {
if (dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = max(maxLen, i - start);
}
return maxLen;
}
dict is just the name that was used for this vector<int>, first parameter is the the size of vector, second is value that should be assigned to all of its positions.
This is one of the possible ways to use its constructor, check the example on this page.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Im trying to opptimise this peice of code as its a small section of a longer code for speed rather than memory. How best would I do that. I was thinking to use set the v_vtx vector to be able to just to define the chitemp array.
double chitemp[nvert1][2];
for (int i=0;i<nvert1;i++){
chitemp[i][1]=v_vtx[i];
chitemp[i][0]=chi2->at(v_vtx[i]);
}
for (int k = 0; k < nvert1; k++){
for( int p = k+1; p < nvert1; p++){
if( chitemp[k][0] > chitemp[p][0]){
swap(chitemp[k][0], chitemp[p][0]);
swap(chitemp[k][1], chitemp[p][1]);
}
}
}
edit:
Im trying to sort chi2 (double) into order and know which v_vtx (int) links to the chi2 value
You could instead store your values as pairs (using std::array is optional, but offers a richer interface than an inbuilt array):
std::array<std::pair<double>, nvert1> chitemp;
for (size_t i = 0; i < nvert1; ++i) {
chitemp[i].second = v_vtx[i];
chitemp[i].first = chi2->at(v_vtx[i]);
}
Then, use...
std::sort(chitemp.begin(), chitemp.end());
...instead of your (inefficient) home-grown bubble-sort.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create some variables in a loop,e.g.
for(int i = 0; i < _vector.size(); i++) //_vector is a vector struct
{
auto v = _vector.at(i);
auto xi = get_name(v); //how to create x0,x1,x2,x3.....dynamically in this loop
}
anyone knows how to do it like that?
thanks very much!
If you want to reference these variables as x1, x2, etc., it would be better to create a vector to store these.
The code below is written to support integers, however, this can be replaced with another data type.
vector<int> x;
for(int i = 0; i < _vector.size(); i++) //_vector is a vector struct
{
auto v = _vector.at(i);
x.push_back(get_name(v)); //sets the value of x.at(0), x.at(1)...
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to write a simple console game.
I want to refresh console 30 times per second. Usually this is not a problem, but this time I'm working with an array of size 30x30, and printing it using two loops is simply not fast enough.
I noticed that
<code>printf( "%s\n", myarray );</code>
is quick enough, but it doesn't work properly with 2d arrays.
Is there a function that will make my array appear "instantly" on screen?
I'm using this function to print my array:
void draw(char screen[32][31]){
for (int x=0;x<32;x++){
for (int y=0;y<31;y++){
cout<<screen[x][y];
}
cout<<endl;
}
}
This should be faster:
void draw(char screen[32][31]){
for (int x = 0; x < 32; x++){
cout.write(screen[x], 31);
cout << '\n';
}
cout << flush;
}
As noted in a comment above, endl is the wrong way to insert a newline, because it also flushes the stream and so removes the benefits of buffering done by the I/O library. See endl vs '\n' for more information.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a 2-Dimensional array whose elements I typically want to access like this:
val = my_array[row][col];
But I also have need to access elements using their absolute index from time to time, where the row and column are not known. The "absolute" index of a given element can be computed as follows:
abs_idx = row*numCols + col;
I am thinking of achieving this as follows
mydatatype *my_array_abs = new mydatatype[numRows*numCols];
mydatatype **my_array = new mydatatype*[numRows];
for (int ii=0; ii<numRows; ii++)
{
my_array[ii] = &my_array_abs[ii*numCols];
}
Is this an appropriate way to achieve my goal, or should I expect to run into any problems or inefficiencies?
To achieve what you want you need to change your line:
mydatatype *my_array;
To:
mydatatype **my_array = new mydatatype*[numRows] ;
Note: I see only one issue with this approach that, you required continuous memory chunk.
Otherwise your approach is perfectly fine.
I'd be tempted to use std::vector to avoid all the hazards of raw arrays:
#include <vector>
#include <iostream>
typedef int mydatatype;
typedef std::vector<std::vector<mydatatype> > myvectortype;
int numCols = 10;
int numRows = 100;
mydatatype& at_absolute(myvectortype& v, int index) {
return v[index / numRows][index % numRows];
}
int main() {
myvectortype my_array(numRows, std::vector<mydatatype>(numCols, 0));
my_array[1][2] = 31;
std::cout << at_absolute(my_array, 102) << '\n';
}