473,756 Members | 9,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector<pair> .. more

Consider:
# include <iostream>
# include <string>
# include <map>
# include <bitset>
# include <vector>

using namespace std;

typedef unsigned char* ADDR_TYPE;
typedef unsigned int uint_type;
uint_type const NUM_PROC(4);

int CNum2(2);
// Go back to Josuttis and see if I got the right candidate for teh Job
// for now a vector of pairs ...
typedef std::pair < unsigned int, std::vector<int > > Mypair;
typedef std::vector< Mypair > myVecPair;

void card2_func( myVecPair& refVec ) // give this function the array
{
int mask(0x10);
if (refVec.size())
{
refVec.clear();
}
uint_type SZ(NUM_PROC * CNum2);
int size(5);

for (int idx(0); idx < 2; ++idx)
{
uint_type lMask = 0x00000001 << (NUM_PROC*(CNum 2-1));
uint_type nContor(NUM_PRO C * (CNum2 - 1));

bool found_match(fal se);
for (; nContor < SZ; nContor++)
{
if (mask & lMask)
{
int val = mask & lMask;
refVec[idx].second.push_ba ck(nContor);
printf(" %d\n", nContor);
found_match = true;
}
lMask <<= 1;
}
mask += 0x1;
if ( found_match )
{
refVec[idx].first = size;
}
size += 1;
// cout << " OK " << std::endl;
}
}

int main()
{
myVecPair mV;
card2_func(mV);
for (int idx(0); idx < mV.size(); ++idx)
{
std::cout << mV[idx].first << std::endl;
for (int jdx(0); jdx < mV[idx].second.size(); ++jdx)
{
// std::cout << mV[idx].second.[jdx] << std::endl;
}
}
return 0;
}

At issue: I'm having difficulties trying to access the mV 'second'
elements within main. See commented out line.
2/
I need to pull out the texts again for a pointer refresher. I'm using
unsigned char* for doing pointer arithmetic.

So now:
typedef unsigned char* ADDR_TYPE;
int Var(0xD0000000) ; // known address on PCI bus.
ADDR_TYPE ptr = (ADDR_TYPE)Var;

ptr is a unsigned char pointer. The variable Var is an integer. When
I cast Var to ptr. I'm getting the address of Var. But Var is is an
integer. So the cast to ptr would need enough memory to hold a Var.
I'm tring to determine when ptr would overflow?

Dec 14 '05 #1
7 9148
It seems to me like there should not be a . between "mv[idx].second"
and "[jdx]". If you do want to have a ., I think it should be
".operator [](jdx)".

I don't know anything about your second question.

Dec 15 '05 #2
ma740988 wrote:
Consider:
# include <iostream>
# include <string>
# include <map>
# include <bitset>
# include <vector>

using namespace std;

typedef unsigned char* ADDR_TYPE;
typedef unsigned int uint_type;
uint_type const NUM_PROC(4);

int CNum2(2);
// Go back to Josuttis and see if I got the right candidate for teh Job
// for now a vector of pairs ...
typedef std::pair < unsigned int, std::vector<int > > Mypair;
typedef std::vector< Mypair > myVecPair;

void card2_func( myVecPair& refVec ) // give this function the array
{
int mask(0x10);
if (refVec.size())
{
refVec.clear();
}
This is just redundant. If you need to clear, then clear. If it's empty
already, 'clear' has no effect.
uint_type SZ(NUM_PROC * CNum2);
int size(5);

for (int idx(0); idx < 2; ++idx)
{
uint_type lMask = 0x00000001 << (NUM_PROC*(CNum 2-1));
uint_type nContor(NUM_PRO C * (CNum2 - 1));

bool found_match(fal se);
for (; nContor < SZ; nContor++)
{
if (mask & lMask)
{
int val = mask & lMask;
refVec[idx].second.push_ba ck(nContor);
printf(" %d\n", nContor);
found_match = true;
}
lMask <<= 1;
}
mask += 0x1;
if ( found_match )
{
refVec[idx].first = size;
}
size += 1;
// cout << " OK " << std::endl;
}
}

int main()
{
myVecPair mV;
card2_func(mV);
for (int idx(0); idx < mV.size(); ++idx)
{
std::cout << mV[idx].first << std::endl;
for (int jdx(0); jdx < mV[idx].second.size(); ++jdx)
{
// std::cout << mV[idx].second.[jdx] << std::endl; .. ^^^
.. What's the dot doing there?
}
}
return 0;
}

At issue: I'm having difficulties trying to access the mV 'second'
elements within main. See commented out line.
What kind of difficulty?
2/
I need to pull out the texts again for a pointer refresher. I'm using
unsigned char* for doing pointer arithmetic.

So now:
typedef unsigned char* ADDR_TYPE;
int Var(0xD0000000) ; // known address on PCI bus.
ADDR_TYPE ptr = (ADDR_TYPE)Var;

ptr is a unsigned char pointer. The variable Var is an integer. When
I cast Var to ptr. I'm getting the address of Var.
Really? What proof do you have?
But Var is is an
integer. So the cast to ptr would need enough memory to hold a Vr.
What?
I'm tring to determine when ptr would overflow?


Is that a question? Please start it with a verb or a special word.

V
Dec 15 '05 #3

Ok.. Thanks all.
Victor, I have to re-think what the heck I was asking on that second
question. I don't do too well with trying to convey my thoughts when
dealing with C++.

Dec 15 '05 #4
On 2005-12-14, ma740988 <ma******@gmail .com> wrote:
Consider:
# include <iostream>
# include <string>
Not used.
# include <map>
You do not need <map> for std::pair. #include <utility> instead.
# include <bitset>
Not used.
# include <vector>

using namespace std;

typedef unsigned char* ADDR_TYPE;
typedef unsigned int uint_type;
uint_type const NUM_PROC(4);
I'm confused by your naming conventions. In particular, all-caps
should be reserved for evil macros.
int CNum2(2);
// Go back to Josuttis and see if I got the right candidate for teh Job
// for now a vector of pairs ...
typedef std::pair < unsigned int, std::vector<int > > Mypair;
typedef std::vector< Mypair > myVecPair;
And yet another name convention appears. Why is it Mypair and
myVecPair? That makes no sense.

Seriously, the wildly varying convention for names makes this
code harder to understand that it needs to be.
void card2_func( myVecPair& refVec ) // give this function the array
{
int mask(0x10);
if (refVec.size())
{
refVec.clear();
}
Just clear it if you want to clear it. There's no need to check
it's already empty.
uint_type SZ(NUM_PROC * CNum2);
int size(5);

for (int idx(0); idx < 2; ++idx)
{
uint_type lMask = 0x00000001 << (NUM_PROC*(CNum 2-1));
uint_type nContor(NUM_PRO C * (CNum2 - 1));

bool found_match(fal se);
for (; nContor < SZ; nContor++)
{
if (mask & lMask)
{
int val = mask & lMask;
refVec[idx].second.push_ba ck(nContor);
printf(" %d\n", nContor);
found_match = true;
}
lMask <<= 1;
}
mask += 0x1;
if ( found_match )
{
refVec[idx].first = size;
}
size += 1;
// cout << " OK " << std::endl;
}
}
I hope there's no bug in the above, because it doesn't seem worth
the efford to understand it.
int main()
{
myVecPair mV;
card2_func(mV);
for (int idx(0); idx < mV.size(); ++idx)


I'm hating your use of the constructor syntax for initializing
built-in types. I strongly encourage the idiomatic "int idx = 0"
to avoid confusing-looking code. In addition, your going to find
yourself accidentally declaring functions some of the time if you
keep it up. ;-)

In the case of your nested loops, using iterators instead of
indexing will be a greatly improvement, and speed things up, too.

You probably wnat '\n' instead of std::endl in this code, as a
std::endl after every output defeats the whole purpose using a
buffered output stream.

for(myVecPair:: iterator i = mV.begin(); i != mV.end(); ++i)
{
std::cout << i->first << '\n';
for (vector<int>::i terator j = i->second.begin() ;
j != i->second.end() ; ++j)
{
std::cout << *j << '\n';
}
}

--
nCerutti (or is it Ncerutti or nC or NEIL_CERUTTI? I shall let
fate decide) ;-)
Dec 15 '05 #5

Neil, I realize what you're saying, nonetheless realize it's just 'test
code'. The names used dont reflect what my real code. In other
words, the superfluous string etc.. etc. is only there becasue I was
doing 'other test' that's not shown. The naming convention and all -
is again part of test code. In my real code I have meaningful names.
I agree, that it - perhaps - makes things slightly harder to read at
the newsgroup level but the intent at the newsgroup level is to:
1. post the smallest amount of - preferably compilable source.
2. highlight the arrea of code where I'm having difficulties.

Love the comments that you and Victor allude to with respect to:
1. No need to check size. Call clear directly.
|| I'm hating your use of the constructor syntax for initializing
built-in types.
This a new one for me but, I'll take that into consideration.

|| In the case of your nested loops, using iterators instead of
indexing will be a greatly improvement, and speed things up, too
Agreed

|| In particular, all-caps should be reserved for evil macros.
I never fully understood this. Is this a programming rule or ??

Dec 15 '05 #6
ma740988 <ma******@gmail .com> wrote:
|| In particular, all-caps should be reserved for evil macros.
I never fully understood this. Is this a programming rule or ??


Not a rule, but a convention that helps to avoid name conflicts. If you
stick to this convention, then the likelihood of a macro unexpectedly
changing your code can be minimized.

--
Marcus Kwok
Dec 15 '05 #7
On 2005-12-15, ma740988 <ma******@gmail .com> wrote:

Neil, I realize what you're saying, nonetheless realize it's
just 'test code'. The names used dont reflect what my real
code. In other words, the superfluous string etc.. etc. is
only there becasue I was doing 'other test' that's not shown.
The naming convention and all - is again part of test code. In
my real code I have meaningful names. I agree, that it -
perhaps - makes things slightly harder to read at the newsgroup
level but the intent at the newsgroup level is to:
1. post the smallest amount of - preferably compilable source.
2. highlight the arrea of code where I'm having difficulties.
OK. But remember that clearer code will elicit clearer help. ;-)
When putting together sample code to post, following exemplary
coding practices will imrove your chances of getting a helpful
reply.
Love the comments that you and Victor allude to with respect to:
1. No need to check size. Call clear directly.
|| I'm hating your use of the constructor syntax for initializing
built-in types.
This a new one for me but, I'll take that into consideration.


It's just a matter of convention and opinion; others may not
agree with me. But I found

for (int idx(0); idx < mV.size(); ++idx)

confusing at first, even though its meaning is clear enough after
briefly thinking about it. I just don't want to need to think
about it.

--
Neil Cerutti
Dec 15 '05 #8

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

Similar topics

20
17832
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
3
7545
by: Gary Wessle | last post by:
Hi is it ok to do this? if not, then how for (vector<pair<char,double::const_iterator it = v.begin(); it != v.end(); ++it) switch (*it->first){... thanks
6
1518
by: asdf | last post by:
It is best to show me some examples. Thanks a lot.
2
7283
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
18
2507
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the container. I have written both these operators as non-friend functions.
0
9482
marktang
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...
0
9292
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10062
Oralloy
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...
0
9901
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9878
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,...
1
7282
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6551
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();...
0
5322
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2694
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.