Click here to Skip to main content
15,887,338 members
Everything / Vector

Vector

vector

Great Reads

by Clayton Rumley
There are those who think I am crazy, and this just might be the proof.
by Sergey Alexandrovich Kryukov
Another variant of the cross-platform replacement for all those office presentation applications in a single file, and now this file is JavaScript
by Arthur V. Ratz
Optimizing the performance of the large-sized matrices QR factorization and eigendecomposition, using Schwarz-Rutishauser algorithm
by Arthur V. Ratz
Compute the Levenshtein distance of literal strings effectively by using the Wagner-Fischer (two matrix rows-based) algorithm.

Latest Articles

by Nicolas DESCARTES
What are vector databases?
by Sergey Alexandrovich Kryukov
Another variant of the cross-platform replacement for all those office presentation applications in a single file, and now this file is JavaScript
by Arthur V. Ratz
Compute the Levenshtein distance of literal strings effectively by using the Wagner-Fischer (two matrix rows-based) algorithm.
by spore123
This is a fast multi-threaded quick-sort based algorithm

All Articles

Sort by Updated

Vector 

20 Feb 2024 by merano99
The problem is probably that Visual Studio 2010 does not support C++11 initialization lists for vectors. Although I cannot test with VS2010, I could imagine that this task could be solved with a template function. Here, a two-dimensional vector...
19 Feb 2024 by 0x01AA
There is I direct way without first declaring a helper vector: vector > myVector { /* Element 0 */ {1, 2, 3, 4}, /* Element 1 */ {5, 6, 7, 8}, /* Element 2 */ ...
19 Feb 2024 by dj4400
Hi all, I want to initialize a vector of vectors in c++ vs2010 I tried to declare a table of doubles double dTable[3][4] = { 1,2,3,4, 5,6,7,8, 9,0,-1,-2}; and then declaring a vector of...
9 Feb 2024 by Nicolas DESCARTES
What are vector databases?
12 Sep 2023 by Brennon Nevels
While I'm aware that this is just a simple thing to do, I've tried to make this into a function. While mine kind of works, I'm more interested in how other people would go about it. #include #include #include ...
12 Sep 2023 by Greg Utas
If the order of elements in the vector isn't important, an efficient way to do this is to move the last element into the slot of the element being erased. This is how I manage the list of open sockets for the poll function, and I wrote a simple...
11 Sep 2023 by CPallini
With C++ 20, the single line std::erase(shoppingList, choice); would do the trick. See std::erase, std::erase_if (std::vector) - cppreference.com[^].
11 Sep 2023 by Rick York
That is done use the erase method of the vector class : https://cplusplus.com/reference/vector/vector/erase/[^]. It can be a very expensive operation to perform if deleting from anywhere except the tail end of the vector. If deletions are going...
4 Sep 2023 by Sergey Alexandrovich Kryukov
Another variant of the cross-platform replacement for all those office presentation applications in a single file, and now this file is JavaScript
13 Aug 2023 by merano99
It seems to me that templates might not be the right way for the desired functions. I would first overload only the output operator. There are several ways to do this. Instead of the function as before, a class VectorPrinter can be created that...
13 Aug 2023 by Brennon Nevels
My friend and I are building a text-based game. But the problem so far is I cannot figure out how to resolve this problem. The goal was to make two functions. One to print a vector and another to find a specific element within. #include...
13 Aug 2023 by Rick York
The function should look more like this : template void printVector( const std::vector & vec ) { for (int i = 0; i
28 Jun 2023 by Quinten Bakker (QB)
So I have a class node(name, vector, distance, prev) and i want to sort it by distance. And i have used some methods like sort or with a operator but nothing seems to work. Is there any other way to to sort a vector of classes? What I have...
28 Jun 2023 by Richard MacCutchan
I have simplified your node class and the following code builds and runs using Microsoft C++ compiler: #include #include #include #include class node{ public: std::string name; int...
28 Jun 2023 by k5054
I added a main function the above code vis: // .. QB's code up to here #include int main() { vector test; sort( test.begin(), test.end(), [](const node &a, const node &b){ return (a.distanceToStart
11 Feb 2023 by merano99
The errors in the merge_sorted() function have already been addressed. But it would be unusual to do this task with a loop anyway. One possibility would be to use the combination of insert() and sort(). But both together are already done with the...
11 Feb 2023 by mrtyty
process finishes with incorrect exit code. #include #include using namespace std; vector merge_sorted(vector a, vector b) { for (int i = 0; i
10 Feb 2023 by CPallini
You have the power of the standard library, at your disposal: us it! See: std::merge - cppreference.com[^]. #include #include #include using namespace std; int main() { vector vec1{3, 5, 7, 12, 16, 19}; ...
10 Feb 2023 by k5054
Take a look at the loop inside merge_sorted(): for(int i = 0; i
24 Jan 2023 by k5054
If you are using linux you can approximate a C++ vector using a dynamic memory buffer stream (aka a memstream). Here's an example of how you might go about that: #include #include int main() { int *ints = NULL; /* a...
24 Jan 2023 by Chillzy
I'm trying to convert C++ code to C and it was all fine until I got to the .push_back() function. After some research, I found out this wasn't a function in C. How do I write a function in C that replicates the uses of the .push_back() function? ...
24 Jan 2023 by CPallini
Quote: ll the code is in C++ and not C, which is a problem because I work in C, due to it being better than C++ (rather structs and unions than classes and templates and whatever bs C++ has to offer). Use C++ if you can. C++ is multi-paradigm...
24 Jan 2023 by Richard MacCutchan
You cannot do it easily, as this is a method of the STL vector class. So you would need to write a complete set of functions to manage whatever data you are trying to collect. Which all begs the question, why on earth are you trying to convert...
7 Dec 2022 by Freddie Francis
This method finds all Alien objects in the AlienList object’s Vector whose humanoid field has the value false. It stores all matching Alien objects in a new Vector that is then returned from the method. The method must also remove all the...
7 Dec 2022 by OriginalGriff
This is the same question you posted 3 days ago: I need this method to return an empty vector if there are no matching alien objects[^] Please do not repost questions, it doesn't improve the chances of you getting an answer, it just wastes the...
16 Sep 2022 by Arthur V. Ratz
Compute the Levenshtein distance of literal strings effectively by using the Wagner-Fischer (two matrix rows-based) algorithm.
25 May 2022 by spore123
This is a fast multi-threaded quick-sort based algorithm
16 Mar 2022 by honey the codewitch
Get your data on, even on platforms without a reliable STL implementation using these simple but critical classes.
22 Feb 2022 by lock&_lock
(I posted this on CodeReview as well, I hope it's ok. I would love to get different perspectives & suggestion across different platforms). I am following a question on a programming challenge website (here's the original reference). What I have...
21 Feb 2022 by lock&_lock
I have to make several subvectors with lengths following elements of another vector For example : vector A{2,3} // Vector A has 2 elements, therefore I need to make 2 subvectors A[0] = 2, make a new subvector with length of 2, example : B...
21 Feb 2022 by CPallini
You need a containers for your subvectors. You might, for instance, use a vector of vectors: #include #include using namespace std; int main() { vector v{2,3}; vector > sv; // the sub-vectors ...
3 Jan 2022 by Arthur V. Ratz
Optimizing the performance of the large-sized matrices QR factorization and eigendecomposition, using Schwarz-Rutishauser algorithm
11 Dec 2021 by John Brg
I have two vectors A,B and I want to merge them to a vector C but I want to preserve any duplicates in each vector while discarding duplicates between the two. For example if I have A{1,2,3,4,2,5} and B{7,8,2,2,2,3,3} I want C to be...
11 Dec 2021 by John Brg
Well it seems std::set_union does this. Nothing else is needed.
27 Jun 2021 by Proton Vpn
Hello there.. I have an vector drawable that I have been using it for an ImageView inside my Project. Now I want to change the ImageView's height and width to custom dp but when i do it the content inside it also changes it's width and height....
10 Apr 2021 by Bhavini Amin
I am currently writing a programme to calculate the one day VaR using the historical simulation method. Within the programme I am first calculating the daily returns of the prices which the excel formula is Close_price_today -...
17 Mar 2021 by Member 15104493
Hey! I have created an array that has 50 places. When the places are full, I want it to constantly expand with 1 place in the array, but it does not work... I need to use array and can not use List.. And I can not use array.resize... Please...
17 Mar 2021 by OriginalGriff
The best solution is to not use an array - use a List instead. Internally, it uses an array for storage, but externally it appears as expandable. This may help as to why your solution is a very bad idea from a performance point of view:...
17 Mar 2021 by Richard MacCutchan
Use Array.Copy Method (System) | Microsoft Docs[^].
20 Nov 2020 by whataweird
hi, this code won't compile correctly using std::sort and a struct. How do I solve this? #include #include #include #include struct StudentDataTypes { std::string name{}; int grade{}; }; int...
20 Nov 2020 by k5054
You need to tell sort how to compare the two structs. since there's no default comparison for non basic types. Do you want to sort by name or by grade, or by grade then by name, or ...? Probably the easiest way is to add an operator
20 Nov 2020 by Richard MacCutchan
The sort function does not know how to sort a StudentDataTypes structure. You must provide a comparator, see functions | Microsoft Docs[^].
3 Nov 2020 by CPallini
You could also overload the insertion operator ...
3 Nov 2020 by Member 14982907
#include #include using namespace std; struct CD_TYPE { string title, artist; int year; double cost; } ; void printData(struct &collection[]); int main() ...
3 Nov 2020 by Joe Woodbury
In addition to Richard's solution, I'd: 1) make collection const. void printData(const vector& collection); 2) Iterate the collection using: for (const auto& item : collection) 3) Add void print() const method to CD_TYPE. 4) Use...
3 Nov 2020 by Richard MacCutchan
You are trying to pass a vector reference to the printData function, so you should use the correct declaration: void printData(vector& collection); Also in your printData function you are trying to print 5 entries, but the...
15 Oct 2020 by Josh_0101
Hi, I'm trying to define a structure (Brace initialization, has constructor) for a Vector function, but it failed to be compiled and showed these errors. error C2059: syntax error : ',' error C2334: unexpected token(s) preceding '{'; skipping...
15 Oct 2020 by Richard MacCutchan
That syntax is in a later version of the compiler than the one provided by VS2008. You should upgrade to VS2019.
6 Sep 2020 by Издислав Издиславов
Showcase for simple techniques for XAML button styling
23 Jun 2020 by Greg Utas
Replacing its erase() function
28 Mar 2020 by CPallini
A diagonal starting at (row,0) includes all items (row+k,k) where k=0,1,.., ROWS-row-1. In a similar way, a diagonal starting at (0,col) includes all items (k,col+k) where k=0, 1, .., COLS-col-1. The task is competed if you iterate on rows: row...
28 Mar 2020 by R0ber1t
I'm looking for a C++ way using vector to get all the diagonals of a (square) matrix, represented as a 2d vector. matrix = [ [1,2,3,4], [5,1,2,3], [9,5,1,2]] But I have trouble coming up with a way to generate all the diagonals....
19 Jan 2020 by Manujaya Premathilaka
I am writing a program in c++ using eclipse. I have a vector which needs to store pointer objects. When I compile the program, it crashes saying the program has stopped working. How can I fix this error? //declaring pointer to TopStock class TopStock* stock = new TopStock(row[0], row[1],...
19 Jan 2020 by Patrice T
Quote: When I compile the program, it crashes saying the program has stopped working. Posting 2 lines of code that refer to mystery other code is no help to understand what you did wrong. Your code do not behave the way you expect, or you don't understand why ! There is an almost universal...
19 Jan 2020 by OriginalGriff
We can't tell - we don't have access to your data, or to your code while it's running - and that what you need to work out why. Heck, we don't even know what the error message is, and that's normally very, very relevant! So, it's going to be up to you. Fortunately, you have a tool available to...
10 Dec 2019 by Md Razeenuddin Mehdi
Given a string S with length N. Choose an integer K and two non-empty subsequences A and B of characters of this string, each with length K, such that: >> A=B, i.e. for each valid i, the i-th character in A is the same as the i-th character in B. >> Let's denote the indices of characters used...
10 Dec 2019 by Member 14684499
Try These test Case 4 4 anxa 6 ababcb 4 abcd 1 a Output should be 1 4 0 0
8 Dec 2019 by OriginalGriff
Compiling does not mean your code is right! :laugh: Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send. So now...
6 Sep 2019 by Stefan_Lang
The function GetIntArrayFromCharArray creates a copy, therefore your calls to begin() and end() refer to two different arrays! This is reflected literally in the error message - all you need to do is read and understand it. ;-) [..] (cut away revised parts of the solution - see below) ...
5 Sep 2019 by Danny96
Hello, I am trying to concat vectors when I get data from cable in my while loop. I search for copy method in C++ array a lot and I saw examples like my code which are supposed to run correct, however I am getting error as in title. I put breakpoints and I observed; ...
13 Aug 2019 by Rick York
Try this : int main() { std::ifstream input; input.open( "Data.CS.SFSU" ); std::string line; std::vector def; while( ! input.eof() ) { getline( input, line, '|' ); def.push_back( line ); } input.close(); return 0; }
13 Aug 2019 by Member 14558341
#include #include #include #include #include #include #include using namespace std; int main() { string line; vector def; map; ifstream input; if (!input) { cout
13 Aug 2019 by Patrice T
Looks like your code have more than 1 problem: This should be better: input.open("Data.CS.SFSU"); // 1 open file if (!input) { // 2 check if failed cout
13 Aug 2019 by CPallini
Quote: def.pop_back(line);//too many arguments in a function call (red line on "line") The correct method is push_back, see vector::push_back[^]. The pop_back method is used for deleting the last element of the vector.
13 Aug 2019 by k5054
That should be def.push_back(line). pop_back() deletes the last element of the vector. See vector::pop_back - C++ Reference[^] Additionally map; should be generating a compiler error, as it does not declare anything.
5 Aug 2019 by Member 14549814
I want a vector of functions that would look something like this: vector>v; but obviously this didn't work... something like this: #include using namespace std; int f1(int n){ return n*2+2; } string f2(string n){ return n+'!'; } int main(){...
5 Aug 2019 by Stefan_Lang
Don't do this. It's probably doable with variadic templates, but judging by your code you already have enough trouble without templates. Moreover, putting functions (or rather references to functions) into a container eliminates readability, makes your code hard to read and prone to errors. ...
4 Aug 2019 by Rick York
Here's one way it can be done. typedef UINT( WINAPI * ThreadFunc )( PVOID ); typedef struct { ThreadFunc function; PVOID argument; } OneFunction; int main() { std::vector functions; OneFunction f; // set the members of f here ...
5 Aug 2018 by Member 12779695
I want to find the number of distinct prime number of a given number, I am passing a array to the function and then multiplying all the elements of the array to generate the number, I am getting BUS ERROR for some test cases What I have tried: int Solution::solve(vector &A) { long...
5 Aug 2018 by Patrice T
This code is weird: for(int i=0;(i
9 May 2018 by Member 13816159
Hello, I am writing a program that allows users to input a music album and store them into vectors. Then the program asks the user whether to borrow it. To do that I tried using .erase of the given vector. However the vector function is defined in a constructor of a class and the modification is...
9 May 2018 by Richard MacCutchan
You have not explaine the problem. Although I suspect that your class declaration entry for Borrow1 is wrong. You have the following in your class: public: Album(); void Borrow1() {}; // Borrow1 is an empty function. }; And you later define Album::Borrow1() with a proper body. In my test this...
26 Mar 2018 by Vishal Bhatia0112
I have a Vector of structure that is sorted on based of a variable. For ex: struct Client { string name; int sm_index; int client_id; int user_id; } Sorted on based of sm_index. How can i find and fetch the data of that struture whose sm_index matches our target structure. ...
26 Mar 2018 by Rick York
Here's an example of how to use binary_search : bool Compare( const Client& lhs, const Client& rhs ) { return ( lhs.client_id clientVec ) { return binary_search( clientVec.begin(), clientVec.end(),...
25 Mar 2018 by KarstenK
Thats sounds poor, because iteration is one main standout feature of the vector container class. If your vector is sorted I would suggest the faster binary search but I think that you may consider this in a later stage of your project.
11 Mar 2018 by Member 13720753
I do not understand this question: "In main method creates a (1) vector (not vector object) of the class type Animals.This should be of size 50" What I have tried: i have tried to create it in this way: Vector myAnimals = new Vector(50); did i do it right? Thank you
6 Feb 2018 by rafaelfinalcut10
The code below – it's a skeleton of a program operating on the dynamic collection of data. The idea is to use a structure containing two fields: the first stores the number of elements in collections, and the second is the actual collection (a dynamically allocated vector of ints). As you can...
4 Feb 2018 by CPallini
Quote: int *temp; temp = new[]; Let's fix your starting point. The above statemens should be int * temp; temp = new int[col.elno + 1]; Now your temp vector as the right number of items. You have to Copy all the col.elements items into temp. Add element at the end of temp. ...
4 Feb 2018 by OriginalGriff
As I told you when you posted this earlier today, we do not do your work for you. Deleting the question and reposting it in the hope we won't notice is not a good idea - it won't get your work done for you, but it will annoy people. Read the instructions carefully, and think about what you...
8 Jan 2018 by CodeBlooded
So, I what I want is to collect maximum element from each row(in a 2D vector) and push it into a new vector. And if the new vector is sorted I want the sum of all vector elements. I tried it but the code is showing error : "cannot convert 'std::vector to 'int' assignment" also, is my code...
8 Jan 2018 by Patrice T
You try to solve this problem Contest Page | CodeChef[^]. As usual CodeChef challenges are not for beginners, and unfortunately you have read the requirements too fast and misunderstood it. All details count. Someone else have asked for help, read answers given, they should help you to...
22 Dec 2017 by wseng
The program firstly load the data from sql to vector. Then it will store the all the vector element to array. When I compile, I get these errors. What's wrong here? java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.Vector.copyInto(Vector.java:192) ...
11 Dec 2017 by Leo Chapiro
Just try to find object vector C++, that's all!
17 Nov 2017 by Patrice T
Quote: i think that that code must work and do not know why it doesn`t. Launch the debugger, on every step, check that everything is what it should. for (auto i = n, j =0; i > ind+1; i--) Are you using the maximum value in vector as the ending position in loop ? Unless this is carefully...
17 Nov 2017 by Member 13527256
I have to add a new element and move some others in my vector in this program \/, but vector elements do not want to change at all. #include #include using namespace std; int main() { int n; cin >> n; vector v; for (auto i = 0, j = 0; i
21 Sep 2017 by Mwater07
I would like to write a program that stores nonzero elements of 2 different vectors into 2 different linked lists with their indexes. Then would like t to calculate the dot product and displays the result. I am struggling to read in vector values with indexes into a link list. Please advice ...
21 Sep 2017 by Patrice T
The code is not complete, you should use the debugger to see what your code us doing There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality. When you don't...
19 Sep 2017 by Mwater07
I am trying write C++ programs that compute the dot product of two given vectors. In vectors a and b only nonzero elements will be stored into array of structures(row and col). Each time I am getting irrelevant results. The correct result should be 50. Please advice. Thank you in advance. ...
19 Sep 2017 by CPallini
You are trying to complicate a simple matter. Try #include #include using namespace std; // Please note: error handling (e.g. a.size()!=b.size()) left as exercise int dot(const vector & a, const vector &b) { int dp = 0; for (size_t n = 0; n
19 Sep 2017 by Jochen Arndt
There are three errors in your code. The first two ones: You have a fixed array size of 10 for row and col but are accessing 11 elements (out of bound access) and did not initialise all elements: for (int i=0; i
10 Sep 2017 by raushanaj5
I am trying to send images to server. Earlier I had a simple function void fileSend(const char* fpath) This function is able to send one image file to the server. But, I am supposed to send many images to the server at once, that is why I went for vector. I overloaded the previous method...
10 Sep 2017 by Jochen Arndt
There is no clear concept in your code and you are mixing C and C++ library functions for similar tasks (e.g. using C++ and C streams for file operations). I don't see actually where and why your code fails, but here are some tips: Your code does not check for errors. You should check the...
10 Sep 2017 by KarstenK
the path sound weired: string path = "C:\\Images:\\"; with the second ":" Resolve this and debug the code.Maybe the overloaded call isnt resolved as you wish. Try to rename one function.
10 Sep 2017 by apurv625
I am changing the signature of a function having default argument char* to vector in C++. The role of function with default argument char* is to take out the image name of an image file, then read that image file and send to the server. This function is only capable of sending one image...
10 Sep 2017 by CPallini
The straightforward way to do that is overloading the old function (and call it): void fileSend(vector fnames) { for (int k = 0; k
31 Jul 2017 by Member 13338695
#include "stdafx.h" #include #include #include #include #include #include #include #include using namespace std; class Medicine{ private: string productName; double productPrice; public:...
31 Jul 2017 by CPallini
Have a look at Serialization[^]. I suggest you to add serialization support to your Medicine class (e.g. load/save methods) and then iterate over the vector items in order to serialize the whole container.
8 Jun 2017 by Member 13248267
#include #include void jum(int *p, int m, int n) { int i; if(m==(n-1)) return; if(n%2==0) { for(i=m;i