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 2 years ago.
Improve this question
I have these vectors as follows
vector< vector< Arc * > * > _adjacences;
vector <int> list[_adjacences.size()];
vector <int> listD[_adjacences.size()];
vector < vector <int> > matrix( _adjacences.size(), vector<int>(_adjacences.size(),0 ));
vector < vector <int> > shortPath( _adjacences.size(), vector<int>(_adjacences.size(),0 ));
I want to make the adjacency list to an adjacency matrix
Arc contains these:
int sommetArrive;
int longueur;
string nom;
I tried to make two vectors one for the distance and the other for the peeks
for (unsigned i = 0; i < _adjacences.size(); i++){
for (auto j : *_adjacences[i]){
list[i].push_back(j->sommetArrive);
listD[i].push_back(j->longueur);
}
}
Then I make the adjacency matrix, THIS IS WHERE I'M DOING THINGS WRONG
for (int i = 0; i < _adjacences.size(); i++) {
for (auto j : list[i]){
for (auto k : listD[i]){
matrix[i][j] = k;
}
}
}
Instead of getting this:
0 0 0 0 0 0 0 0 0 0 0 0 0 120 0 62
0 0 253 0 0 0 0 0 0 0 204 0 0 0 0 0
0 53 0 12 0 0 105 0 0 0 0 0 0 0 0 0
0 0 15 0 38 0 0 108 0 0 0 0 0 0 0 0
0 0 0 93 0 123 0 0 113 0 0 0 0 0 0 0
0 0 0 0 158 0 0 0 0 118 0 0 0 0 0 0
0 0 97 0 0 0 0 17 0 0 0 87 0 0 0 0
0 0 0 103 0 0 3 0 53 0 0 0 73 0 0 0
0 0 0 0 153 0 0 33 0 113 0 0 0 0 0 0
0 0 0 0 0 55 0 0 91 0 0 0 0 72 0 0
I end up with this
0 0 0 0 0 0 0 0 0 0 0 0 0 62 0 62
0 0 204 0 0 0 0 0 0 0 204 0 0 0 0 0
0 105 0 105 0 0 105 0 0 0 0 0 0 0 0 0
0 0 108 0 108 0 0 108 0 0 0 0 0 0 0 0
0 0 0 113 0 113 0 0 113 0 0 0 0 0 0 0
0 0 0 0 118 0 0 0 0 118 0 0 0 0 0 0
0 0 87 0 0 0 0 87 0 0 0 87 0 0 0 0
0 0 0 73 0 0 73 0 73 0 0 0 73 0 0 0
0 0 0 0 113 0 0 113 0 113 0 0 0 0 0 0
0 0 0 0 0 72 0 0 72 0 0 0 0 72 0 0
Where the same number is being repeated instead of passing to the next element k
What did I do wrong ?
Notice that for every i you use the same list listD[i]. The inner loop:
for (auto k : listD[i]){
matrix[i][j] = k;
}
will assign all values in listD[i], one by one, to matrix[i][j], until the final value remains. This is the same as writing matrix[i][j] = listD[i].back().
You probably need to replace the inner loop with matrix[i][j] = listD[i][j].
Related
I would like to print a '>' sign next to an array which is able to move up and down when different things are inputted.
I am able to print a 12x12 array of 0s but when I try to print a '>' in line 7, the '>' is inputted into the array and the row itself is shifted over instead of the array being kept uniform (i.e. the '>' must be independent to the array).
I have tried creating two arrays of the '>' and the 0s but I am not able to print two things horizontally next to each other.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int row = 0;
while (row < 12){
int col = 0;
while (col < 12){
printf(" 0");
col = col + 1;
}
printf("\n");
row = row + 1;
if (row == 7) {
printf(">");
}
}
return 0;
}
output:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
intended output:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
thank you !
To display a ">" sign before your matrix you can use a conditional statement specifically for the required line number.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
for (int i = 0; i < 12; i++)
{
if(i == 7) // conditional statement
printf("> "); // for ">" followed by
else // a space to maintain
printf(" "); // the structure of matrix
for (int j = 0; j < 12; j++)
{
printf("0 ");
}
printf("\n");
}
return 0;
}
Hope it helps!!!
I am trying to convert a Format_Grayscale8 image to Format_ARGB32 (or any other 8bpp RGB format). I tried to use QImage::convertToFormat and a color table but I can't make it work. The input is a small 8bit gray image (PGM format text string).
QImage img;
img.loadFromData(gray_map.toLatin1());
QImage heatmap = img.convertToFormat(QImage::Format_Indexed8, color_gradient.getColorMap());
Doing it manually works fine (pixel per pixel):
// gray_img format is QImage::Format_Grayscale8
QImage createHeatMap(QImage gray_img)
{
QImage heatmap(gray_img.width(), gray_img.height(), QImage::Format_ARGB32);
// custom heatmap, size() is 256
const QVector<QRgb> color_map = color_gradient.getColorMap();
// Default color
heatmap.fill(color_map[0]);
for (int y = 0; y < gray_img.height(); y++)
{
for (int x = 0; x < gray_img.width(); x++)
{
const uchar* img_ptr = gray_img.bits();
int offset = x + y*gray_img.bytesPerLine();
uchar gray_pix = *(img_ptr + offset);
// just in case
if (gray_pix < color_map.size())
heatmap.setPixel(x, y, color_map[gray_pix]);
}
}
return heatmap;
}
QImage img;
img.loadFromData(doppler_map.toLatin1());
QImage heatmap = createHeatMap(img);
I am interested in a simpler and more efficient solution. Thanks!
EDIT
Here is the code to generate the color gradient:
// Heatmap color lookup table generator, inspired from:
// http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients
#include <QColor>
class ColorGradient
{
private:
struct ColorPoint
{
float r, g, b;
float val; // Position of our color along the gradient (between 0 and 1).
ColorPoint(float red, float green, float blue, float value)
: r(red), g(green), b(blue), val(value)
{
}
};
std::vector<ColorPoint> mColors; // An array of color points in ascending value.
uint mTableSize = 0;
QVector<QRgb> mColorMap;
// hidden
ColorGradient();
public:
inline auto getColorMap()
{
return mColorMap;
}
ColorGradient(uint table_size)
{
createDefaultHeatMapGradient(table_size);
}
//-- Places a 6 color heapmap gradient into the "color" vector
#define CF64(val) ((float)(val) / 64.0)
#define CF256(val) ((float)(val) / 256.0)
void createDefaultHeatMapGradient(uint table_size)
{
mTableSize = table_size;
mColorMap.clear();
mColors.clear();
// ascending order
mColors.push_back(ColorPoint(0, 0, CF256(96), CF64(00))); // Dark Blue
mColors.push_back(ColorPoint(0, 0, 1, CF64(06))); // Blue
mColors.push_back(ColorPoint(0, 1, 1, CF64(22))); // Cyan
mColors.push_back(ColorPoint(1, 1, 0, CF64(39))); // Yellow
mColors.push_back(ColorPoint(1, 0, 0, CF64(55))); // Red
mColors.push_back(ColorPoint(CF256(159),0, 0, CF64(63))); // Dark Red
QColor color;
// Generate the color table
for (uint n = 0; n < table_size; n++)
{
float value = (float)n / (float)table_size;
bool found = false;
for (int i = 0; i < mColors.size(); i++)
{
ColorPoint &currC = mColors[i];
if (value < currC.val)
{
ColorPoint &prevC = mColors[std::max(0, i - 1)];
float valueDiff = (prevC.val - currC.val);
float fractBetween = (valueDiff == 0) ? 0 : (value - currC.val) / valueDiff;
float r = (prevC.r - currC.r)*fractBetween + currC.r;
float g = (prevC.g - currC.g)*fractBetween + currC.g;
float b = (prevC.b - currC.b)*fractBetween + currC.b;
color.setRgbF(r, g, b);
mColorMap.append(color.rgb());
found = true;
break;
}
}
if (!found)
{
color.setRgbF(mColors.back().r, mColors.back().g, mColors.back().b);
mColorMap.append(color.rgb());
}
}
}
};
And a sample text image file:
P2
40 17
64
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 7 7 7 7 7 7 7 7 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 13 13 13 13 13 13 13 13 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 14 14 14 14 14 14 14 14 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 23 23 23 23 23 23 23 23 23 23 23 23 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 13 13 13 13 13 13 13 13 13 13 13 13 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The trick is to first convert to QImage::Format_Indexed8 without any color map, and set it in a separate call:
QImage heatmap = img.convertToFormat(QImage::Format_Indexed8);
heatmap.setColorTable(color_gradient.getColorMap());
After this, you can make a second conversion to QImage::Format_RGB888 or whatever you need.
I would like to create a new variable "type" based on conditions being true across multiple variables, but I have too many variables (~100) to type. I am using SAS Studio v 9.4.
My data is set up similar to this:
DATA have;
INPUT id
a_var_a a_var_b a_var_c a_var_d a_var_e
b_var_a b_var_b b_var_c b_var_d
c_var_a c_var_b c_var_c d_var_d;
DATALINES;
01 1 0 0 0 0 0 0 0 0 0 0 0 0
02 0 1 0 0 0 0 0 0 0 0 0 0 0
03 0 0 1 0 0 0 0 0 0 0 0 0 0
04 0 0 0 1 0 0 0 0 0 0 0 0 0
05 0 0 0 0 1 0 0 0 0 0 0 0 0
06 0 0 0 0 0 1 0 0 0 0 0 0 0
07 0 0 0 0 0 0 1 0 0 0 0 0 0
08 0 0 0 0 0 0 0 1 0 0 0 0 0
09 0 0 0 0 0 0 0 0 1 0 0 0 0
10 0 0 0 0 0 0 0 0 0 1 0 0 0
11 0 0 0 0 0 0 0 0 0 0 1 0 0
12 0 0 0 0 0 0 0 0 0 0 0 1 0
13 0 0 0 0 0 0 0 0 0 0 0 0 1
;
Run;
"type" is coded as:
1 If any of the group a vars (a_var:) are equal to 1
2 If any of the group b vars (b_var:) are equal to 1
3 If any of the group c vars (c_var:) are equal to 1
else equal to 0
I thought it would be as simple as:
Data want;
Set have;
If a_var: = 1 then type = 1;
Else If b_var: = 1 then type = 2;
Else If c_var: = 1 then type = 3;
Else type = 0;
Run;
However I keep getting an error code because I am not allowed to group the variables.
I tried doing the same thing with an array but I am still unable to arrive at a solution:
Data want;
Set have;
Array a (*) a_var:;
Array other (2,4) b_var: c_var:;
do i = 1 to dim(a);
If a(i) = 1 then type=1;
end;
do i = 1 to 4;
If other (1,i) = 1 then type=2;
If other (2,i) = 1 then type=3;
Else type=0;
end;
drop i;
Run;
I am trying to create 3 categories of the "type" variable (0,1,2, and 3) based on how the conditions are met.
Thank you!
This is the code eventually worked.
DATA have;
INPUT id
a_var_a a_var_b a_var_c a_var_d a_var_e
b_var_a b_var_b b_var_c b_var_d
c_var_a c_var_b c_var_c c_var_d;
if whichn (1, of a_var: ) =>1 then type=1;
else if whichn (1, of b_var: ) =>1 then type=2;
else if whichn(1, of c_var:) =>1 then type=3;
else type = 0;
DATALINES;
01 1 0 0 0 0 0 0 0 0 0 0 0 0
02 0 1 0 0 0 0 0 0 0 0 0 0 0
03 0 0 1 0 0 0 0 0 0 0 0 0 0
04 0 0 0 1 0 0 0 0 0 0 0 0 0
05 0 0 0 0 1 0 0 0 0 0 0 0 0
06 0 0 0 0 0 1 0 0 0 0 0 0 0
07 0 0 0 0 0 0 1 0 0 0 0 0 0
08 0 0 0 0 0 0 0 1 0 0 0 0 0
09 0 0 0 0 0 0 0 0 1 0 0 0 0
10 0 0 0 0 0 0 0 0 0 1 0 0 0
11 0 0 0 0 0 0 0 0 0 0 1 0 0
12 0 0 0 0 0 0 0 0 0 0 0 1 0
13 0 0 0 0 0 0 0 0 0 0 0 0 1
14 0 0 0 0 0 0 0 0 0 0 0 0 0
;
Run;
I don't think the prefix: shortcut can be used for something like this.
Instead I suggest you use macros to generate the code you need based on DICTIONARY.COLUMNS (see data set column names into macro variable(s) for an example).
You can generate conditions like a_var_a=1 or a_var_b=1 or a_var_c=1 or a_var_d=1 or a_var_e=1 using something like this (untested):
/* preferably enclose this in a macro and declare the macrovariable as %local mvGroupAIsSet; */
proc sql noprint;
select cats(name, '=1') into :mvGroupAIsSet separated by ' or '
from dictionary.columns
where name like 'a_var_%' /* don't remember if you need to escape the underscores */
and libname = 'WORK'
and memname = 'HAVE';
quit;
Then use this in your DATA step:
data want;
set have;
if &mvGroupAIsSet then type = 1;
/* etc */
run;
I have a PEM Certificate generated using OpenSSL. The certificate is read using fopen call in C++. The X509 object is then copied into an unsigned char array using memcpy, and the ascii values of the array are displayed.
When I repeat the same process (reading the file, and displaying the ascii values), the results differ. Results differ across executions, as well as in the same execution.
This is how I am reading the certificate
FILE* f_cert = fopen(filename.str().c_str(), "r");
X509 *tempCert = NULL;
PEM_read_X509(f_cert, &tempCert, NULL, NULL);
This is how I display the certificate
memcpy (temp, tempCert, sizeof(Certi));
for (int a=0; a<sizeof(X509); a++){
cout << (int)temp[a] << " ";
}
Output-1:
16 216 64 1 0 0 0 0 128 223 64 1 0 0 0 0 160 223 64 1 0 0 0 0 0 0 0 0
1 0 0 0 208 232 64 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255
255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Output-2:
16 216 212 0 0 0 0 0 128 223 212 0 0 0 0 0 160 223 212 0 0 0 0 0 0 0 0
0 1 0 0 0 208 232 212 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255
255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
But when I read the certificate's MD5, it stays the same. What could be an explanation for this?
I need to insert this certificate into a Bloom Filter.
Thanks in advance.
I have the following data written on a file. I want to neglect all the zeros in the beginning and the but the starting from 181 in an array each number in a cell so I could use it easily.
I know how to put data in an array but how could I neglect all these zeros ??
0 177 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 181 98 1 2 28 0 104 93 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 166 6 0 0 78 5 0 0 164 145 181 98 1 2 28 0 80 97 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 169 6 0 0 78 5 0 0 147 117 181 98 1 2 28 0 56 101 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 173 6 0 0 81 5 0 0 134 109 181 98 1 2 28 0 32 105 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 181 6 0 0 85 5 0 0 126 137 181 98 1 2 28 0 8 109 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 182 6 0 0 87 5 0 0 109 101
I am not sure I understand your question, so I will post multiple answers. Choose the one that fits with your problem's description.
Case 1: ignore everything before(or before and including) 181:
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream in("input.txt");
std::vector<int> vec;
int reached_181 = 0, x;
while(in >> x) {
if(x == 181) reached_181 = 1;
if(reached_181) vec.push_back(x);
// if you also want to neglect 181 then just change the order of the two commands
// if(reached_181) vec.push_back(x);
// if(x == 181) reached_181 = 1;
}
for(std::vector<int>::size_type i=0; i<vec.size(); ++i) {
std::cout << vec[i] << " ";
}
return 0;
}
Case 2: ignore every zero before 181
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream in("input.txt");
std::vector<int> vec;
int reached_181 = 0, x;
while(in >> x) {
if(x == 181) reached_181 = 1;
if(reached_181 || x) vec.push_back(x);
}
for(std::vector<int>::size_type i=0; i<vec.size(); ++i) {
std::cout << vec[i] << " ";
}
return 0;
}
Case 3: ignore all the zeroes in the input file
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream in("input.txt");
std::vector<int> vec;
int x;
while(in >> x) {
if(x) vec.push_back(x);
}
for(std::vector<int>::size_type i=0; i<vec.size(); ++i) {
std::cout << vec[i] << " ";
}
return 0;
}
Try this:
#include <fstream>
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v;
std::fstream out("out.txt"); // name of your file
bool hit;
for (int n; (out >> n);)
{
if (n == 181 && !hit)
hit = true;
if (!hit)
if (n)
v.push_back(n);
if (hit)
v.push_back(n);
}
typedef std::vector<int>::const_iterator iter_type;
for (iter_type it = v.begin(); it != v.end(); ++it)
std::cout << *it << std::endl;
}