C++ :: Maps - Referencing Individual Members Numerically (as With A Vector)

Feb 9, 2014

I have code working, but that is largely because a vector that I am using as part of a structure that is the first of two elements of a MAP (with the second being an integer that is assigned later) is serving in its original, purely vector form, to fill out a set of selections on a DOS screen (option 1, 2, 3, etc.).

I do use the MAP, because it is a simple means of referencing the integer value using a word that is part of the structure, and is probably appropriate, saving me a search through the vector of words and calling up the value at the same location in the vector of words in a vector of corresponding numerical values. I DO feel that what I'm doing to produce the selection list is something of a "cheat" because I do not know how to access the same data within the structure component of the MAP by simply using a similar numerical reference (e.g. "MAP[1].structure.word", or would that be "MAP.structure.word[1]" ????) to print out the words in the structure in the map as choices on the screen.

I don't want to change or expand the contents of the map, just access and print each one in a simple loop with a number from the loop counter to the left of each word.

View 4 Replies


ADVERTISEMENT

C++ :: Searching Vector With Maps?

Sep 19, 2014

Currently im creating a simple phone directory. I am having a problem when searching the vector. It only lets me search for the exact key in the directory when I need to to search for strings that are close to the name. For example when I search "car" it will state that its not in the directory when it should pull up Carr, Derek and Carr David.

#include <string>
#include <map>
#include <vector>
#include <ostream>
#include <iostream>
int main() {
std::map<std::string, std::vector <std::string> > directory;

[code]....

View 1 Replies View Related

C++ :: Compare Data Members Of Objects Store In Vector

Mar 6, 2014

Overview of problem : I am using std::vector to hold objects of Subject. Now this vector contains lots of objects( with lots I mean 10-20 objects at max) . These objects have string member values like category and sub_category. Both category and sub_category can have string which can be same of other objects's sub_category & category.

Issue: Now I want my std::vector to have only those objects whose's sub_category are unique. If category is not unique that's not a problem .

Secondly if we found 2 objects having same sub_category then we have to delete one of them from the vector. we will delete it based on some rules example

Rules for deleting are if

i) instance of Subject ->category = " Land " OR if category = "Jungle" then delete other duplicate object ,
ii) if above condition doesn't match then delete either of them.

I am wondering , how would I compare the sub-items from the vector . For example. I have class say Subject

class Subject {
public :
// some constructors,
// functions to get ., set category and sub category
std::String get_sub_category()
std::string get_category();
private:
std::string category;
std::string sub_category;
}

I have vector which stores object of Subjects. Example : vector<Subject> copy_vector;

Now what I want is to delete the object from vector that has same sub_category I am not looking for source code buT i need a starting point,? Example:

copy_vector[0] = Animal object that has sub_category Tiger
copy_vector [1] = Animal object with Lion as sub category
copy_vector[2] = Forest object with sub_category Tiger

What I want is to based on some conditions(which I can do ) remove either Forest or Animal object containing Tiger. But for that how would I do comparison? I have written the function and have checked it.

std::vector< Subject >copy_vector;
// copy_vector contains all the objects of Subject with redundant sub_category
for( std::vector< Subject >::iterator ii = copy_vector.begin() ; ii != copy_vector.end() ; ++ii ) {
sub_category = ii->get_sub_category();

[code] ....

View 1 Replies View Related

C++ :: Computational Calculations - Numerically Solve Quantum Harmonic Oscillator

Apr 20, 2012

My task I am trying to accomplish is write a program for C++ that numerically solves a quantum harmonic oscillator. I have a routine that I am trying to implement into this program that iterates through a matrix and finds it eigenvalues, however, I do not know how to put it all together to make it work. Code is below:

Code:
//Compiler flags and numerical constants
#include <iostream>
#include <math.h>
#define L 10.0
#define N 200
using namespace std;

[Code] ....

View 1 Replies View Related

C++ :: How To Reserve Memory For Maps

Nov 26, 2014

I have a situation where map elements will be inserted all throughout a continuous loop. I know that there's no reserve function for maps like there are for vectors, but I know exactly the maximum amount of elements there will be in the map, I just don't know the value of each individual map.

Would it be possible if I just inserted the maximum amount of elements in the beginning of the program, and change the value of each/any individual element later on? For example:

std::map<int, std::string> map;
//There will be a maximum of 200 elements in this map
for (int i = 0; i < 200; i++) {
map[i];
}

//Change the value of individual elements as I wish

Is it more efficient than initializing on declaration if initializing will be done often?

View 2 Replies View Related

C++ :: Construct Maps Initialization

May 5, 2013

I've a problem with the map construct. I wrote this class in which there are maps:

class Features {
private:
map<const string,GenericFeatureContainer*> featuremap;
map<const string,GenericFeatureContainer*>::iterator it;
int size;
public:
}

And I have the following constructor:

Features::Features() {
map<NULL,NULL> featuremap;
it=NULL;
size=0;
}

Is that correct? otherwise what is the correct syntax?

View 2 Replies View Related

C++ :: Getting Segmentation Fault Using Maps?

Dec 7, 2014

GameMaster.h
#includes ...
class GameMaster
{

[Code].....

I get the segmentation fault whenever it tries to insert the first player.. however whenever i do this :

void MakeSquad(char type, string name, int coordX, int coordY, char dir, int squad)
{
GameSpace::MapSquadType MapOfSquads;​
MapOfSquads.insert(make_pair(name , Squad(squad)); // Squad( int ) just converts the int squad number to the Enum.
}

I do not get any errors or segmentation faults..

View 5 Replies View Related

C/C++ :: How To Load Maps From A Txt File

Dec 29, 2014

i have made this first level in this ascii game, and i realized that if i want to continue, i must rewrite the switch statement and write thousands of lines of code. how can i load a map from a txt file?

#include <iostream>
#include <windows.h>
using namespace std;

[Code].....

View 3 Replies View Related

C++ ::  referencing In The Loop

Jul 30, 2013

My problem is as following.

I have an 2 arrays named: abee1 , abee2.

I have loop that want to call them one by one and print data of an them in each step. It means that I want to reach the name of arrayBee1 to arrayBee2 in the loop for printing its data. I made the name of each one by using strings ( abee1 , abee2) in the loop.

#include<iostream>
#include<string>
#include <sstream>
using namespace std ;
int abee1 [4] [4] = {{5,0,40,30},{6,0,21,47},{7,0,17,63},{8,0,31,62}};
int abee2 [4] [4] = {{1,0,37,52},{2,0,49,49},{3,0,52,64},{4,0,20,26}};

[Code] ....

I know something is wrong with the "string *a = & arrayname;" part of code and after that.

View 2 Replies View Related

C/C++ :: Regarding Pointers To Structures And Referencing?

Mar 24, 2015

concepts on pointers to structures and referencing for the following two lines.

//address of the variable "struct UIP_IP_BUF" is assigned as srcipaddr
uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr,
//data type "uip_lladdr_t" pointer points to the address of array an "nd6_opt_llao" with size UIP_ND6_OPT_DATA_OFFSET
(uip_lladdr_t *)&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET]

View 5 Replies View Related

C# :: Referencing DLL Again When Already Referenced In Another Project

Aug 17, 2014

I am building a log4net wrapper project where I reference log4net.dll. When calling this code through a static instance to log messages the new project also wants me to reference log4net.dll even though I have a reference to my logging project which in turn has the reference..

View 2 Replies View Related

C++ :: Difference In By Referencing And By Pointer In Parameters?

Aug 6, 2013

void func(float arg); // arg is supposed to be in type, with no referencing, just a copy of it

void func(float &arg); // arg is supposed to be in, out type, with something contained in it already

void func(float *arg); // arg is supposed to be in-out type, with nothing contained in it already and can be NULL

void func(const float& arg) // arg is supposed to be in type, with nothing contained in it already and cannot be NULL

Am I all correct?

View 2 Replies View Related

C++ :: Why Not Referencing Other Parameters In Default Values Allowed

Nov 24, 2013

[URL] ....

#include <iostream>
#include <vector>

void f(std::vector<int> const &v, std::vector<int>::const_iterator it = v.end()) {
} int main() {
f({}); }
prog.cpp:4:73: error: local variable ‘v’ may not appear in this context
void f(std::vector<int> const &v, std::vector<int>::const_iterator it = v.end())

Why is this not allowed? (I mean, what is the reasoning for defining the standard this way?)

In C++14/C++17 we will have a unified way to represent end iterators without an instance of the container, but currently I just have to hope my implementation accepts a default-constructed iterator as an end iterator.

View 4 Replies View Related

C++ :: Net Pay By Pass Referencing A Gross Pay Overloaded Function

Oct 23, 2014

I need to return taxes paid and net pay by pass referencing a gross pay overloaded function. Are the values returned from calling the overloaded function file stream objects? Can they be passed simply through a pass-by-reference function?

//Read Data from File, gather info and calculate pay, output data to file
while(counter < x) {
inFile >> last_name >> first_name >> hours_worked >> hourly_pay;
outFile << first_name << " " << last_name << " ";
outFile << calculate_gross_pay(hours_worked,hourly_pay);
counter++;
outFile<<endl;

[code].....

View 8 Replies View Related

C++ :: Doubly-Linked List And Referencing Errors?

Oct 4, 2014

We had to convert a single-line text editor which uses arrays to one that uses OOP and double-linked lists and I have been doing it in steps. I have, for the sake of convenience, put my headers, implementation and main all in one file.

I'm compiling this program in Hercules (the getch function uses C code).

I keep getting the error from the compiler saying "Undefined Symbol" for functions:

insertNode(char, Node);
deleteNode(Node*);
insertHead(char);

The full message is: ld: fatal: Symbol Referencing Errors. No output written to a.out collect2: ld returned with 1 exit status.

// A fake single line text editor program.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <termios.h>
//#include "main.h"
typedef char Letter;

[code]....

View 3 Replies View Related

C# :: Using Info From MySQL Tables And Referencing Results

May 8, 2014

Basically I have a few tables in my Database.

So I have one table that is like so:

Item_ID, Name, Parent ID.
1 , jeff , 5

and another table like this:

Parent_ID, parent_Name
5,jackson

What I would like to happen, when I run my code is that I'll get the following

Item_ID, Name, parent_Name
1 , jeff , jackson

DataSet DS = new DataSet();
if (this.OpenConnection() == true) {
mySqlDataAdapter = new MySqlDataAdapter("select Item_ID, NAME, Parent_ID from table1, table2", connection);
mySqlDataAdapter.Fill(DS);
dataGridView1.DataSource = DS.Tables[0];
//close connection
this.CloseConnection();
}

So this spit out parent ID = 5.

I've not worked with Dataset before and I was just wondering what is the best approach? Can this be done via a SQL command or will I have to replace the value(5) with the string(jackson) using a large IF loop to search and replace.

View 1 Replies View Related

C :: Changing Cents Into Individual Denominations

Sep 16, 2013

I haven't actually learned how to use arrays yet but I know the basic principle of it and decided to try and implement one to improve my code. I'm sure the thing is a bug ridden mess but I would particularly like to point your attention to the function sortDenomination(). Is what I am trying to do in the loop possible?

The reason why I want to do it this way and not with if statements for each denomination is because I can then easily apply this to the second part of the assignment which sees us split a double into dollars and cents and then process each separately. All I would have to differ for each is change the money_loop variable to include the last two denominations in the array.

Code:
#include <stdio.h>
/*Reads cents input from the user.*/
void getCents(int &read_cents) {
printf("Please enter the amount of cents between 5-95:

[Code] .....

View 10 Replies View Related

C :: Take Integer And Splits It Into Individual Numbers

Oct 9, 2013

I'm supposed to write a code that takes an integer and splits it into individual numbers e.g. 234 becomes 2 3 4 .the individual numbers are then passed on to a function that accepts only one number at a time

All i could think of was this,but its a very bad method coz i dont know how long a number would be inputed into "Angle_in_degree"

Code:
int Angle_in_degree,a,b,c
c= Angle_in_degree %10;
b=Angle_in_degree/10 %10;
a=Angle_in_degree/100 % 10;

function(a);
function(b);
function(c);

View 2 Replies View Related

C++ :: Counting Individual Vowels In A Sentence

Dec 23, 2013

I'm trying to get this program to work that will count the frequency of each vowel.

#include <iostream>
#include <string>
using namespace std;

int main(){
char sent[81];
cout << "Type up to an 80 character sentence." << endl;
cin.getline(sent, 81);

[Code] .....

View 2 Replies View Related

C :: Manually Save Data In Individual Bits

Mar 18, 2013

I'm a C beginner. I have questions, let say if I have a structure of message containing of Id, Length, and 3 bytes of data, and I want to manually saved in individual bits of data, how can I do that? I confused on how to use typedef struct and union. Below is the sample code:

typedef struct {
uint8_t Id;
uint8_t Length;
uint8_t DataField[3];
}AA;

[Code] .....

By the way, the DataField is declared as above. It's not necessary for a data to be 24 bits. It can be bit 0 - bit 8, it can be bit 10 - bit 15 and so on. So, for each case, I want to ignore the other bits.

View 5 Replies View Related

C :: Replacing Individual Chars In A String Array?

Nov 16, 2013

I'm trying to right the function that puts the correct letters in the right positons being reported from my previous compareletter function for my game of hang man and its not updating the word in progress array. Here's my compare letter function that's working correctly:

Code: //function that returns the index of the letter that the user has guessed or Code: //-1 if the letter isn't in the word
int CompareLetter(char array[], char guess, int numLetters)
{
int i;

[Code....

However, this isn't changing any of the position of the asterisks in the word in progress array positions 0-3.

View 7 Replies View Related

C++ :: Assigning Colors To Individual Characters In Array?

Aug 31, 2014

Basically I am making a console RPG (isn't every beginner nowadays?) and I am having an incredible amount of trouble assigning specific characters their own color. e.g. monsters being red, cash being green, etc.

I took out Left, Right, and Down controls because of character constraint.

#include <iostream>
#include <windows.h>
//%%%%%%%%%%%%%% COLOR CALL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void setcolor(unsigned short color) {
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);

[code]....

View 8 Replies View Related

C# :: Unable To Change The Individual Keyboard Layouts

Jan 14, 2014

i am developing a multi-lingual application allowing users to change language as well as keyboard layouts (through combobox). I am able to change the culture of application successfully but i'm unable to change the individual keyboard layouts e.g. for English (United States), im unable to change layouts from QWERTY (default) to Dvorak - Left hand etc... I don't just want to change the input language, i was also want to be able to type in different keyboards. Here's the code (e.g. for English language), the imports etc are added already..

CultureInfo TypeOfLanguage = CultureInfo.CreateSpecificCulture("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = TypeOfLanguage;
InputLanguage l = InputLanguage.FromCulture(TypeOfLanguage);
InputLanguage.CurrentInputLanguage = l;

View 6 Replies View Related

C :: Creating Black Bitmap - How To Set Color Of Individual Pixel

Nov 2, 2014

creating a bitmap. Our professor wants us to creat a black 100*100 bitmap.

Code:

#include <stdio.h>
#include <stdlib.h>
#pragma pack(push,2)
typedef struct{

[Code].....

I dont even know where to start. I spend the last 7h infront of my computer and i'm so frustrated cause my professor gave us no information what so ever. At the moment its more of a trial and error process than anything else.

How would i set the color of an individual pixel for examplel or how would i save the file ?

View 1 Replies View Related

C :: Turning String Of Numbers Into Array Of Individual Integers

Mar 2, 2014

In my program, I am fed a string that contains integers such as Code: *str = "45678" and my program is simply supposed to read the number in and store each given number in a separate spot in an integer array. So basically, when my program has finished running it should be stored like:

Code:

arr[0] = 4
arr[1] = 5
arr[2] = 6
arr[3] = 7
arr[4] = 8

The way I have been attempting to do this was just copying it to the array like so:

Code:

arr = malloc(sizeof(int) *(strlen(str));
for (i=0; i<strlen(str); i++) {
a->digits[i] = str[i];
}

however, this just seems to return an impossibly high garbage value when I do. I'm assuming the way I'm trying to store it is 'illegal', but I cant seem to find online a proper way to do it.

View 2 Replies View Related

C++ :: Separate Input Integer Into Its Individual Digits And Print

Apr 18, 2013

Write a full C++ program that inputs three-digit integer, separates the integer into its individual digits and prints the digits separated from one another. For example, if the user types 549, the program should print;

5 4 9

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved