473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Doubt with std::vector

Hello to everybody.
I have a doubt see the code:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iterator>

using namespace std;

class Test
{
private:
vector<int>Number;
string szFileNameInput;
string szFileNameOutPut;
static long posx;
...
public:
Test() : Number(15),szFileNameInput(""),szFileNameOutPut("" ){};
~Test(){};
int ReadFileInput(const string& file, vector<int>& ref);
int WriteFileOutPut(const string& file, vector<int>& ref);
};

long Test::posx = 0;
This code opens an archive with 126 lines with disordered numbers, for
example:
01 12 45 88 99 35 66

and I am trying to make:

int Test::ReadFileInput(const string& file,vector<int>& ref)
{
try
{
ifstream in(file.c_str());
if(!in) throw string("Error 1: ");
string temp;
in.seekg(input,ios::beg);
getline(in,temp,'\n');
input = in.tellg();
in.close();
in.clear();
istringstream is(temp);
istream_iterator<int>ini(is),fim;
vector<int>num(ini,fim);
copy(num.begin(),num.end(),ref.begin());
sort(ref.begin(),ref.end());
temp.clear();
return 0;
}
catch(string msg)
{
cout<< msg << file<< endl;
cin.get();
exit(1);
}
}

int Test::WriteFileOutput(const string& file,vector<int>& ref)
{
try
{
ofstream off(file.c_str(),ios::app);
if(!off) throw string("Error 2: ");
for(unsigned int i = 0; i < NumerosPorSorteio; i++)
{
off << setfill('0') << setw(2) << ref[i] << " ";
}
off << endl;
off.close();
ref.clear();
return 0;
}
catch(string msg)
{
cout<< msg << file << endl;
cin.get();
exit(1);
}
}

But in no line value "00" exists but it appears after the line number
50.
What I am making of made a mistake?

Thanks a lot for help.

Nov 17 '06 #1
2 1493

ro************@gmail.com wrote:
Hello to everybody.
I have a doubt see the code:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iterator>

using namespace std;

class Test
{
private:
vector<int>Number;
string szFileNameInput;
string szFileNameOutPut;
static long posx;
...
public:
Test() : Number(15),szFileNameInput(""),szFileNameOutPut("" ){};
~Test(){};
int ReadFileInput(const string& file, vector<int>& ref);
int WriteFileOutPut(const string& file, vector<int>& ref);
};

long Test::posx = 0;
This code opens an archive with 126 lines with disordered numbers, for
example:
01 12 45 88 99 35 66

and I am trying to make:

int Test::ReadFileInput(const string& file,vector<int>& ref)
{
try
{
ifstream in(file.c_str());
if(!in) throw string("Error 1: ");
string temp;
in.seekg(input,ios::beg);
getline(in,temp,'\n');
input = in.tellg();
in.close();
in.clear();
istringstream is(temp);
istream_iterator<int>ini(is),fim;
vector<int>num(ini,fim);
copy(num.begin(),num.end(),ref.begin());
sort(ref.begin(),ref.end());
temp.clear();
return 0;
}
catch(string msg)
{
cout<< msg << file<< endl;
cin.get();
exit(1);
}
}

int Test::WriteFileOutput(const string& file,vector<int>& ref)
{
try
{
ofstream off(file.c_str(),ios::app);
if(!off) throw string("Error 2: ");
for(unsigned int i = 0; i < NumerosPorSorteio; i++)
{
off << setfill('0') << setw(2) << ref[i] << " ";
}
off << endl;
off.close();
ref.clear();
return 0;
}
catch(string msg)
{
cout<< msg << file << endl;
cin.get();
exit(1);
}
}

But in no line value "00" exists but it appears after the line number
50.
What I am making of made a mistake?
Does it make sense to you that newsgroup readers need to be asking
questions like:
What is NumerosPorSorteio?
In ReadFileInput(...), why is input undefined?
Why is vector Number a member of the class? With a size of 15?
Why are you sorting a vector when a std::set orders elements
automatically?
In WriteFileOutput(...), why are you appending to file?
Since you are passing by reference the filenames to be read and
written, why are you storing them in the class?

Nov 17 '06 #2
This was only one simple example of the problem that I am passing, the
fact is that NumerosPorSorteio was written in my native
language(pt_BR), it means Numbers for drawing, only this. Yes, I am
passing the names of the archives inside of another function so that
the user declares them. In WriteFileOutput() I it adds each interaction
of reading of the vector for an archive of exit already declared by the
user. I go to see its suggestion on the method set() to see where I am
made a mistake, remembering that still I am learning C++, then errors
are common.
I thank the help.

Salt_Peter wrote:
ro************@gmail.com wrote:
Hello to everybody.
I have a doubt see the code:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iterator>

using namespace std;

class Test
{
private:
vector<int>Number;
string szFileNameInput;
string szFileNameOutPut;
static long posx;
...
public:
Test() : Number(15),szFileNameInput(""),szFileNameOutPut("" ){};
~Test(){};
int ReadFileInput(const string& file, vector<int>& ref);
int WriteFileOutPut(const string& file, vector<int>& ref);
};

long Test::posx = 0;
This code opens an archive with 126 lines with disordered numbers, for
example:
01 12 45 88 99 35 66

and I am trying to make:

int Test::ReadFileInput(const string& file,vector<int>& ref)
{
try
{
ifstream in(file.c_str());
if(!in) throw string("Error 1: ");
string temp;
in.seekg(input,ios::beg);
getline(in,temp,'\n');
input = in.tellg();
in.close();
in.clear();
istringstream is(temp);
istream_iterator<int>ini(is),fim;
vector<int>num(ini,fim);
copy(num.begin(),num.end(),ref.begin());
sort(ref.begin(),ref.end());
temp.clear();
return 0;
}
catch(string msg)
{
cout<< msg << file<< endl;
cin.get();
exit(1);
}
}

int Test::WriteFileOutput(const string& file,vector<int>& ref)
{
try
{
ofstream off(file.c_str(),ios::app);
if(!off) throw string("Error 2: ");
for(unsigned int i = 0; i < NumerosPorSorteio; i++)
{
off << setfill('0') << setw(2) << ref[i] << " ";
}
off << endl;
off.close();
ref.clear();
return 0;
}
catch(string msg)
{
cout<< msg << file << endl;
cin.get();
exit(1);
}
}

But in no line value "00" exists but it appears after the line number
50.
What I am making of made a mistake?

Does it make sense to you that newsgroup readers need to be asking
questions like:
What is NumerosPorSorteio?
In ReadFileInput(...), why is input undefined?
Why is vector Number a member of the class? With a size of 15?
Why are you sorting a vector when a std::set orders elements
automatically?
In WriteFileOutput(...), why are you appending to file?
Since you are passing by reference the filenames to be read and
written, why are you storing them in the class?
Nov 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.