What does 'dict' do in c++? [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 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.

Related

How to decide is it a string or not? [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 10 months ago.
Improve this question
I have an array of strings and I'd like to assort the elements by real datatype ("1" -> int; "abc" -> string; "1a" -> string) :
#include <iostream>
#include<vector>
int main() {
std::vector<std::string> number;
std::vector<std::string> str;
std::string arr[5] = {"1","1a","ab","10.1","a2"};
for(int i = 0;i<5;i++){
if(arr[i] /* is string */){
str.push_back(arr[i]);
}
else if(arr[i] /* is int */){
number.push_back(arr[i]);
}
}
return 0;
}
What is the simplest way?
Thanks!
If your goal is to only examine whether a string contains integer/decimal or not, you can try a solution with regexes:
if(std::regex_match(arr[i], std::regex("[-|+]?[0-9]*[.,]?[0-9]+")))
number.push_back(arr[i]);
else
str.push_back(arr[i]);
I'm assuming you're not taking overflow into account because you're only using strings, otherwise if you wanted to convert strings to numbers, you'd have to take that into account and use other solutions.

Optimising c++ arrays and vectors [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
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.

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

Find the closest object from your position on a proper way QT c++ [closed]

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 7 years ago.
Improve this question
I'm trying to find the closest Healthpacks or enemy from a player's position. I have wrote this like:
for(auto &hp : model->getAllHealthPacks()){
if(!hp->getUsed()){
int x = hp->getXPos();
int y = hp->getYPos();
int q = (x*x)+(y*y);
if(q < smallest){
smallest = z;
hpfound = hp;
foundAHp++;
}
}
}
Now I was wondering, this is actually not proper. Are there better and profesional way's to improve my code? (Lambda,...)?
The code in general is not bad, but there is some room for improvement. First, you could make the variable hp a constant since you are not modifying its contents.
You could also create a class to store the coordinates in a single object like this
class Coordinate{
std::pair<int,int> coords;
...
};
The final code could look like this:
for(const auto &hp : model->getAllHealthPacks()){
if(!hp->getUsed()){
Coordinate coord(hp->getCoord());
int q = coord.getX()*coord.getX()+coord.getY()*coord.getY();
if(q < smallest){
smallest = z;
hpfound = hp;
foundAHp++;
}
}
}
You should also rename q to something more clear for future reference.

How do you shrink a larger array into a smaller array? [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
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));