Optimising c++ arrays and vectors [closed] - c++

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.

Related

What does 'dict' do in c++? [closed]

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.

How to dynamically creat variables in a loop(c++) [closed]

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)...
}

Why threads doesn't accept this input? [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
I was doing a project that uses threads to sum some matrix but at the time of creating the threads and adding the params it always shows up the same error. Any ideas?
void sum(std::vector <double>& matrix, std::vector <double>& other) {
for (auto i = 0; i < 15; i++) {
matrix[i] += other[i];
}
}
here it is the operation that threads should do.
std::vector <double>* mat1 = new std::vector <double>[15];
std::vector <double>* mat2 = new std::vector <double>[15];
std::vector <std::thread*> threads;
for (int j = 0; j < 15; j++) {
sum(mat1[j], mat2[j]); //this works;
threads.push_back(new std::thread(sum,mat1[j],mat2[j])); //this dont why?;
}
Thanks in advance
To get it to compile, change:
std::thread(sum,mat1[j],mat2[j])
to:
std::thread(sum, std::ref(mat1[j]), std::ref(mat2[j]))
Example: https://godbolt.org/z/Ek-cnm
But there are multiple problems with your question and code besides just getting it to compile, please listen to what other have said in the comments.

Declare int variable aux a.length = (); Or use o.length () in all loops? [closed]

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 6 years ago.
Improve this question
I wonder which is faster: Say I'm working with some text (30 characters), which would be better? And with a lot of text which would be better?
1-
int tam = text.length();
for(int i=0;i<tam;i++)
{
//something here//
}
2-
for(int i=0;i<a.length();i++)
{
//something here//
}
and also comparing these two:
1-
for (int i = 0; i < b.length(); i++)
{
aux = a.find(b[i]);
if (aux == -1)
{
sucess = 0;
break;
}
else
{
a.erase(aux,1);
}
}
2-
for (int i = 0; i < b.length(); i++)
{
if (a.find(b[i]) == -1)
{
sucess = 0;
break;
}
else
{
a.erase(a.find(b[i]),1);
}
}
Both first are the better approach.
On the first example you are checking if i<a.length() is true on every cycle. That means that you are going to execute a.length() for every iteration. If the variable a is not changed, it is unnecessary and the better approach is to calculate before and use that value.
Note that if the variable a is changed inside, placing i<a.length() might be the correct approach. It depends on your problem.
On the second example it is the same basics. You avoid useless calculations because you won't need to calculate a.find(b[i]) again inside the else.
As a general rule of thumb, as computations get bigger, more complex, and more frequent you want to minimize your unnecessary calculations. This means that storing something that needs to be calculated in a variable may speed up the process.
In both of your examples, for extremely large numbers,
int scratch = big.length();
for(int i=0;i<scratch;i++){
//body//
}
is usually faster.
In the future, general questions like this tend to belong in something like the Code Review Stack Exchange.

template class creation [closed]

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
can someone look at this code and tell me if I am creating the pointer and object correctly please.
int main()
{
Square<int>* originalSquare = new Square<int>(3, 3);
for(int r = 0; r < originalSquare -> rowSize; r++)
{
for(int c = 0; c < originalSquare -> colSize; c++)
{
int num= 0;
originalSquare -> setElement(r, c, num);
}
}
return 0;
}
//quick_sort function
void quick_sort(Square<int>* square)
{
//nothing yet.
}
I keep getting a access violation error for somer reason... Program works fine before I changed this from stack to heap...
Any help will be greatful.
Thanks
This is not the code that shows your problem. Although, I would guess Square allocates a dynamically sized array, and setElement sets it? Can we see your constructor, and the code for setElement?