Best way to split a vector into two smaller arrays

0 votes

I'm attempting to divide a vector into two distinct arrays. A text file's current int vector has one element per line. There is a list of random integers in the text file.

How I intend to approach it:

Right now, I'm thinking of making two ordinary int arrays, iterating over the whole vector, and copying n/2 elements to each array.

What I want to know is:

What is the classiest way to complete my task? I believe I can accomplish this without repeatedly iterating over the vector.

#include <vector>
#include <fstream>
#include <iterator>
#include <iostream>
using namespace std;

vector<int> ifstream_lines(ifstream& fs)
{
  vector<int> out;
  int temp;
  while(fs >> temp)
  {
    out.push_back(temp);
  }
  return out;
}

vector<int> MergeSort(vector<int>& lines)
{
  int split = lines.size() / 2;
  int arrayA[split];
  int arrayB[split];
}

int main(void) 
{
  ifstream fs("textfile.txt");
  vector<int> lines;
  lines = ifstream_lines(fs);

  return 0;
}
Nov 17, 2022 in C++ by Ashwini
• 5,430 points
824 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
498 views
0 votes
0 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
411 views
0 votes
0 answers

Efficient way to return a std::vector in c++

When delivering a std::vector in a function, how much data is duplicated, and how much of an optimization will it be to place the std::vector in free-store (on the heap) and provide a pointer instead, i.e. is: std::vector *f() { std::vector *result = new ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
361 views
0 votes
0 answers

Correct way to work with vector of arrays

Could someone please explain how to work ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points
348 views
0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,340 views
0 votes
1 answer

Is there an easy way to make a min heap in C++?

Use make heap() and its buddies from algorithm>, or priority queue from queue>.  Make heap and friends are used by priority queue. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
676 views
0 votes
1 answer

static memory allocation like dynamic memory allocation

This declaration int r, c; cin >> r >> ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
734 views
0 votes
0 answers

static memory allocation like dynamic memory allocation

int r, c; cin >> r >> c; int ...READ MORE

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
347 views
+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
464 views
0 votes
0 answers

What is the difference between ifstream, ofstream and fstream?

I stumbled discovered ifstream, ofstream, and fstream ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
550 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP