c++ - Building Arrays from Multiline Text Files [closed] - c++

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
So assume I have a file as shown below:
1 2 6 2 3 7
3 7 1 2 3 7
In C++, how can I store the values in two arrays like the ones below?
[1, 2, 6, 2, 3, 7]
[3, 7, 1, 2, 3, 7]

Use two std::vector<int>s and a std::stringstream:
std::vector<int> a, b;
std::string str1, str2;
if (std::getline(file, str1) && std::getline(file, str2))
{
std::stringstream iss(str1);
for (int n; iss >> n; )
a.push_back(n);
iss.clear();
iss.str(str2);
for (int n; iss >> n; )
b.push_back(n);
}

Take a look boost::tokenizer and, like a comment said, use std::vector.

Related

How to reverse a linked list using an STL stack? [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 4 years ago.
Improve this question
I tried searching for a solution from the Internet but I couldn't find one.
Is it possible to reverse a linked list using std::stack<Node* temp>? If so, how?
It certainly is possible with a stack. The algorithm for such a beast (since this is probably class work) is really quite simple when you realise that you pop elements off a stack in the reverse order that you pushed them on.
So, something like this is what you're looking for:
def reverseList (List<Type> &list):
Stack<Type> stack
while not list.empty(): # 1 2 3 ... 9
stack.push(list.getAndRemoveFirst())
while not stack.empty(): # 9 8 7 ... 1
list.pushBack(stack.pop())
But keep in mind that, if you're doing this in a "real" situation (not class work), std::list actually has the facility to reverse lists:
#include <iostream>
using std::cout;
#include <list>
using std::list;
int main () {
list<int> lst;
for (int val: {9, 5, 3, 5, 6, 2, 9, 5, 1, 4, 1, 3})
lst.push_back(val);
cout << "Original list:";
for (const auto &item: lst)
cout << ' ' << item;
cout << '\n';
lst.reverse();
cout << "Reversed list:";
for (const auto &item: lst)
cout << ' ' << item;
cout << '\n';
cout << "\nReverse ITERATOR of\nreversed list:";
for (auto iter = lst.crbegin(); iter != lst.crend(); ++iter)
cout << ' ' << *iter;
cout << '\n';
}
That last section shows how you can iterate over the list in reverse order if that's all that you needed the reversal for. It doesn't actually change the order of the list but instead just allows you to process the items backwards.
The output of that code is:
Original list: 9 5 3 5 6 2 9 5 1 4 1 3
Reversed list: 3 1 4 1 5 9 2 6 5 3 5 9
Reverse ITERATOR of
reversed list: 9 5 3 5 6 2 9 5 1 4 1 3

Explanation why it doesn't return what it should [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have function that gets info about students from a file "Curent.txt".
That's the struct:
struct students {
string CodSt;
string NumeSt;
string PrenSt;
string DenDisc1;
string MedCD1;
string DenDisc2;
string MedCD2;
string DenDisc3;
string MedCD3;
} student[50];
That's the function:
void getStudents() {
int i = 0;
ifstream ifs("Curenta.txt");
while(!ifs.eof()) {
ifs >> student[i].CodSt >> student[i].NumeSt >> student[i].PrenSt >> student[i].DenDisc1
>> student[i].MedCD1 >> student[i].DenDisc2 >> student[i].MedCD2 >> student[i].DenDisc3
>> student[i].MedCD3;
if(!ifs.eof()) {
i++;
cout << i;
}
var = i;
ifs.close();
}
And in "Curent.txt" i have only this:
9 8 1 1 6 1 1 1 1
3 1 1 1 4 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 7 1 1 1 1
My question is why when I output variable "i", the value is just 1..
Thanks in advance.
You should close the inputstream once you finish reading all the data, so out of the loop, not inside.

How can I split integer inside an 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 6 years ago.
Improve this question
In C++, I have the following input: 12345
How can I achieve this output: 1 2 3 4 5 ?
Another example could be:
input: 123
output: 1 2 3
You can simply do this:
int num = 123;
std::vector<int> digits;
while( num > 0 ) {
digits.push_back(num % 10);
num /= 10;
}

Drawing a array [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 8 years ago.
Improve this question
I need help with drawing an array. I basically want to draw the array as it looks in the array instead of on 1 line
Here's my array:
const int MAP_WD = 5;
const int MAP_HT = 5;
int map[MAP_WD * MAP_HT] = {
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
Here's where I'm trying to draw the array:
int y, x;
for (y = 0; y < MAP_HT; ++y)
{
for (x = 0; x < MAP_WD; ++x)
{
}
}
Try printing a new line at the end of each inner loop.
In the inner loop, print the actual elements.
To get the relevant element, simply use y*MAP_WD + x.

How to sort vector<vector<int>>? [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 a vector:
vector<vector<int>> myvector;
How can I sort this vector in an order as in alphabethical order for strings?
e.g. Output:
Unsorted:
5 9 4 12 4
7 9 3 4 7 9
6 5 11
5 8 7 3
5 9 5 1 1
Sorted:
7 9 3 4 7 9
6 5 11
5 9 5 1 1
5 9 4 12 4
5 8 7 3
Just use the std::sort algorithm. The only subtlety is that it sorts in descending order, so you need to change the sorting criteria. Two ways to do this spring to mind.
Use a custom comparison functor, e.g. std::greater:
std::sort(v.begin(), v.end(), std::greater<std::vector<int>>());
Use reverse iterators:
std::sort(myvector.rbegin(), myvector.rend());
The former version makes the intent clearer, whereas the latter may require some head scratching and documentation reading. But the result is the same for both.
Here's a working example:
#include <vector>
#include <algorithm> // for std::sort
#include <functional> // for std::greater
#include <iostream>
int main()
{
// Set up an example vector
std::vector<std::vector<int>> v{{5, 9, 4, 12, 4},
{7, 9, 3, 4, 7, 9},
{6, 5, 11},
{5, 8, 7, 3},
{5, 9, 5, 1, 1}};
// Perform the sort
std::sort(v.begin(), v.end(), std::greater<std::vector<int>>());
// Output the results
for (const auto& i : v)
{
for (auto j : i)
std::cout << j << " ";
std::cout << "\n";
}
}
Output:
7 9 3 4 7 9
6 5 11
5 9 5 1 1
5 9 4 12 4
5 8 7 3