473,324 Members | 2,548 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,324 software developers and data experts.

Map of vectors how? <Long>

Hello everyone,

I've been working with the STL Containers for a little while now, but in
the middle of working on a small "Markov Chain" class I realized that I
wanted to modify my frequency tables, so far I have been using maps like:

std::map<std::string, int> frequency;

A string for the letter combinations and an int for the frequency, but
now I want to add three more frequencies:

1. how many times a letter combination starts a word.
2. how many times a letter combination ends a word.
3. how many times a letter combination is used within a word.

I thought about using a vector within a map but I've no idea how to use
such a data structure?

I know that it has to be declared like so:

std::map<std::string, vector<int>*> frequency;

But how would I add or find data within this container?

Here is some sample code so you know what I'm trying to do:

// CRandomName.h

#include <fstream>
#include <map>
#include <string>

#ifndef CRANDOMNAME_H_
#define CRANDOMNAME_H_

class CRandomName
{

private:

std::string errorMessage;
std::ifstream *fileStreamIn;
std::ofstream *fileStreamOut;
std::map<std::string, int> frequency;

public:

CRandomName::CRandomName();
CRandomName::~CRandomName();
void inputFile(std::ifstream &streamHandle);
void processFile();
void outputList(std::ofstream &streamHandle);
std::string outputName(int minLength, int maxLength);

};

#endif

// CRandomName.cpp

#include "CRandomName.h"

CRandomName::CRandomName()
{

}

CRandomName::~CRandomName()
{

}

void CRandomName::inputFile(std::ifstream &streamHandle)
{

fileStreamIn = &streamHandle;
}

void CRandomName::processFile()
{

while(!fileStreamIn->eof())
{

int stringPosition = 0;
std::string word;
*fileStreamIn >> word;

while ((stringPosition + 1) < word.length())
{
std::string tempValue;
tempValue += (word.substr(stringPosition,1) +
word.substr((stringPosition + 1),1));

std::map<std::string, int>::iterator itr =
frequency.find(tempValue);

if(itr == frequency.end())
{

std::pair<std::string, int> item(tempValue, 1);
frequency.insert(item);

}

else
{

frequency[tempValue] += 1;

}

stringPosition++;
tempValue = "";
}
}

}

void CRandomName::outputList(std::ofstream &streamHandle)
{
fileStreamOut = &streamHandle;
std::map<std::string, int>::iterator itr;

for (itr = frequency.begin(); itr != frequency.end(); itr++)
{
if(itr != --frequency.end()){*fileStreamOut << itr->first << ' ' <<
itr->second << std::endl;}
else{*fileStreamOut << itr->first << ' ' << itr->second;}

}

}

std::string outputName(int minLength, int maxLength)
{

}

Thank you for taking the time to read.
Jul 22 '05 #1
3 10798
"Juicer_X" <br***********@yahoo.ca> wrote in message
news:10*************@corp.supernews.com...
I've been working with the STL Containers for a little while now, but
in the middle of working on a small "Markov Chain" class I realized that I
wanted to modify my frequency tables, so far I have been using maps like:

std::map<std::string, int> frequency;

A string for the letter combinations and an int for the frequency, but
now I want to add three more frequencies:

1. how many times a letter combination starts a word.
2. how many times a letter combination ends a word.
3. how many times a letter combination is used within a word.

I thought about using a vector within a map but I've no idea how to use
such a data structure?

I know that it has to be declared like so:

std::map<std::string, vector<int>*> frequency;
Actually, the following type would be preferable:
std::map<std::string, vector<int> > frequency;
(storing a pointer makes things much more complicated
and error prone, for no benefit).

But if you have a defined set of values/frequencies to store,
why not use a structure or class:
struct UseCounts {
UseCounts() // default-initialize to zero
: all(0), wordStart(0), wordEnd(0), wordAny(0) {}
int all;
int wordStart;
int wordEnd;
int wordAny;
//NB: data members could be private, with member
// functions to increment either of the count types.
};
std::map<std::string, UseCounts > frequency;
But how would I add or find data within this container? Shouldn't be a problem...
Here is some sample code so you know what I'm trying to do: Just a few suggestions.
std::map<std::string, int>::iterator itr =
frequency.find(tempValue);

if(itr == frequency.end())
{

std::pair<std::string, int> item(tempValue, 1);
frequency.insert(item);

}

else
{

frequency[tempValue] += 1;

} All the previous can be replaced with:
frequency[tempValue] += 1; // or ++frequency[tempValue]

std::map::operator[] will automatically insert a default-initialized
(= 0 for int) value for the requested key if it does not exist in
the container yet.

With the struct, this would become:
UseCounts& cnts = frequency[tempValue];
++ cnts.all;
if( isAtWordStart ) ++ cnts.wordStart;
// etc
Thank you for taking the time to read.

You're welcome. I've seen much messier posts getting answered here ;)
hth, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Jul 22 '05 #2
"Juicer_X" <br***********@yahoo.ca> wrote in message news:10*************@corp.supernews.com...
Hello everyone,

I've been working with the STL Containers for a little while now, but in the middle of working on a small "Markov Chain" class
I realized that I wanted to modify my frequency tables, so far I have been using maps like:

std::map<std::string, int> frequency;

A string for the letter combinations and an int for the frequency, but now I want to add three more frequencies:

1. how many times a letter combination starts a word.
2. how many times a letter combination ends a word.
3. how many times a letter combination is used within a word.

I thought about using a vector within a map but I've no idea how to use such a data structure?

I know that it has to be declared like so:

std::map<std::string, vector<int>*> frequency;

But how would I add or find data within this container?
Why not do something like the following?
struct WordStats {
int matchesCount;
int startsWithCount;
int endsWithCount;
int isFoundInCount;
WordStats() : matchesCount(0), startsWithCount(0), endsWithCount(0), isFoundInCount(0) {}
};
std::map<std::string, WordStats> frequency;

Then you would add or access mappings thusly:
WordStats & ws = frequency[std::string("someWord")];
switch (matchKind) }
case matchAll: ++ws.matchesCount; break;
case startsOnly: ++ws.startsWithCount; break;
case endsOnly: ++ws.endsWithCount; break;
case isWithin: ++ws.isFoundInCount; break;
}
Here is some sample code so you know what I'm trying to do: [Cut for space.] Thank you for taking the time to read.


OK.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #3
Thanks for the replies.

I've decided to use my own class, (I was so caught up in the STD
Container classes that I forgot about rolling my own :) ).
Jul 22 '05 #4

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

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
8
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a...
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
2
by: zl2k | last post by:
hi, all I am using a 2 dimensioanl array implemented by vector<vector<long> >. When the row number grows to 8 and I am trying to insert a new row, I got the segmentation error. However, if I...
5
by: Marc Gravell | last post by:
Short version: is it possible to control the endian-ness of BitConverter? - or are there any /framework/ methods like BigEndianBitConverter.GetBytes(long) and .ToInt64()? (I am not after...
4
by: Fredy Halter | last post by:
the following code is not working: std::complex<long doublex(1.,1.); std::complex<long doubleresult(0.,0.); result = 1./x; std::cout << "x = " << x << std::endl; std::cout << "r = " <<...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
2
by: Sean F. Aitken | last post by:
Good afternoon, We have an app that uses a CMap with CString types for values and accepts LPCSTR as the parameter. The object being created is on the heap (created dynamically). A leak detector...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.