I have a written the following code.
#include<iomani p>
#include<fstrea m>
#include<vector >
#include<cctype >
using namespace std;
void PRINT(const vector< vector<string> > &M) {
for (unsigned int r=0; r<M.size(); r++) {
for (unsigned int c=0; c<M[r].size(); c++)
cout << "\t" << M[r][c]<<endl;
cout << endl;
}
}
int main()
{
unsigned int rows , cols;
rows = 1;
string line;
string token;
ifstream file;
file.open("myte xt.txt");
vector< vector<string> > v1(rows);
vector<string> v2;
while(getline(f ile,line)){
v2. push_back(line) ;}
for(unsigned int i = 0 ; i < rows ; i++){
v1[i] = v2;
}
PRINT(v1);
file.close();
system("PAUSE") ;
return 0;
}
What i want to do is read the document in a vector, then sort each line row by row. Lets say for example, i want to put line 1 in 1st row, line 2 in second row..and so on. But this code isnt doing that. Its just taking whole document and pushes into one row. How do i solve my problem?
I am just using vectors. i dont want to use algorithms or maps. JUST VECTORS.
Thanks in advance.
6 2333
Your 2D vector is v1.
That means you have to push_back a vector<string> into v1 for each row.
So, you create a vector<string> and and push_back the strings for row 0.
Then push_back this vector into v1.
Then erase the vector<string> and re-use it to push_back the strings for row 1.
ertc...
Then you can access a particular string using v1[row][col].
So, if i create a for loop, say
for(int row = 0 ; row < v1.size() ; ++row)
{
v1.push_back(ve ctor<string>()) ;
}
is tat goin to work?
How will i generate a loop like tat?
Somethuing like this: -
vector< vector<string> > v;
-
vector<string> v1;
-
-
string data("Hello");
-
v1.push_back(data);
-
string data1("world!");
-
v1.push_back(data1);
-
v.push_back(v1);
-
v1.erase(v1.begin(), v1.end());
-
string data2("from a");
-
v1.push_back(data2);
-
string data3("2D vector");
-
v1.push_back(data3);
-
v.push_back(v1);
-
cout << v[0][0] << v[0][1] << v[1][0] << v[1][1] << endl;
-
But thats for individual strings. What if i am reading it from file?
What's a file got to do with it?
Just read your file data into striong objects and push_back the string objects.
The only difference is I hard-coded "Hello" where you would have read it from the disc. And that has nothing whatsoever to do with string objects in a vector.
You might consider a new thread about how to read a disc file into a string object.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Michael Aramini |
last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++
program. The sizes of the arrays in my intended application are
dynamic in the sense that they are not known at compile time, so I'd
like to use an STL container class template such as valarray or vector
to represent 1D arrays, and valarrays or vectors of valarrays or
vectors to represent 2D arrays.
As I said the sizes of the arrays in my intended application are...
|
by: Computer Whizz |
last post by:
I was reading through Accelerated C++ at work when I read through the first
mention of Vectors, giving us certain functions etc.
Is there any benefit of Arrays over Vectors? Since all Vectors seem to be
(in my eyes at least) are glorified Arrays.
- Now I know there's a bit more difference, but what exactly are the
advantages of Arrays over Vectors (if any)?
Oh, and please keep it in mind I am a real beginner in C++ but can
|
by: Amit |
last post by:
Hello.
I am having some problem organizing a set of vectors. The vectors itself,
could contain a pointer( say integer pointer) or could contain another
object MyClass.
1>So, first of all, is there anyway where I can accomodate both the vector
types into a single set. Something like a set<vector<void*>, my_compare func
>.
Right now, I am having them as two different set dayatypes.
|
by: madhu |
last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx
vector <intv1;
v1.push_back( 10 ); //adds 10 to the tail
v1.push_back( 20 ); //adds 20 to the tail
cout << "The size of v1 is " << v1.size( ) << endl;
v1.clear( ); //clears the vector
I have a few questions:
|
by: wuzertheloser |
last post by:
Use the program skeleton below (starting with #include <stdio.h>)
as the starting point for quiz4. Add the necessary code to the
functions prob1() and prob2(), and add the other 2 functions, as
described in the text below.
You do not need to change anything in main().
In void prob1(void), take a double floating-point number x from
the keyboard and compute the function f(x), which is defined by:
| |
by: Michael O'Brien |
last post by:
Hola~
I have a large array of points (over a million). I would like to
multiply each point in the array by a 4x4 matrix. I keep thinking
there should be an easy way to do this using numpy, but I can't figure
out the mojo to do it. Is that possible?
MO
|
by: nabh4u |
last post by:
Hi,
I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables.
I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I have 3 vector variables in Main(). I want the read function to read the values into the vector using the address I send of the vectors..
Sample code:
//x.cpp
void read(vector <int> a,vector <int> b,vector < vector <int> > c)
|
by: Rob |
last post by:
How would I do this?
I want to be able to handle vectors of many different types of data
and vectors that can contain any number of other vectors of data.
Currently, I have a templated function that handles vectors of vectors
of <typename T(which could be anything from int to vectors of
something else). As well, I have specialized/overloaded functions to
handle the single-level vectors of data (e.g. vector<string>). So the
templated...
|
by: Peter Webb |
last post by:
I am writing some visualisation code for 4 dimensional geometric shapes.
This is to run in the C# .NET XNA environment, so I am cross posting a maths
and C# group.
My software runs just fine for 3D objects - for example I can draw cubes,
tetrahedrons, icosahedrons etc and rotate them on screen. All of the "heavy
lifting" is done by the XNA libraries, which have transformation libraries
to map 3D constructs onto 2D with perspective. Using...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |