C++ :: Accessing Value Of Map When It Is A Vector
Jan 30, 2013
I have the following map: myMap<string,vector<int>>. I now want to look through my map to see if a key exists and if it does, retrieve one of the int values from within that vector. What would be the best way to do this? Right now I have the following:
What would be the best way to look for a key and get one of the int values from its vector value? Right now I'm doing something like this:
[code]
map<string,vector<unsigned int>>::iterator it;
it = wordMap.find(someWord);
if(it == wordMap.end){
cout << "No matching entry";
} else{
// this is where I'd want to access the value (the vector) of the map
}
Is there a better way to do this?
View 2 Replies
ADVERTISEMENT
May 9, 2013
I have a pointer to a vector of objects, each object has an array and a couple of strings. how to access the data in the objects via the pointer.
Best tree::chooseSplit(vector <pgm> *ptr)
{
Best splits;
cout<<ptr[1].filePath; //not working!!!
}
filepath is a string in the pgm object. i also need to be able to access elements in an array that also exists in pgm.
View 2 Replies
View Related
Jul 24, 2013
How I can use class functions in a vector. I have 2 classes:
Gamemode class
bullets class
Inside the Gamemode class I declared: vector<bullets> Bullet and bullets * b.
If the user presses the shoot button a new bullets class will be added to the Bullet vector:
b = new bullets;
Bullet.push_back(b)
Bow I'd like to check all objects in the Bullet vector for the collision() function inside the bullets class.
I already set up a for loop which counts from 0 to the end of the vector:
for( int i=0;i<Bullet.size;i++)
{
}
My idea was to do something like:
if(Bullet[i].collision()) then erase Bullet[i]
But that doesn't work...
View 2 Replies
View Related
May 1, 2015
so lets assume i have a nested vector in a set or vice versa o in a more general form a container in a container example...
Code:
std::set<vector<int> > my_tuple;
How do i access individual elements of lets say the first vector in the set! Or the last element in the 3rd vector?
View 7 Replies
View Related
Oct 20, 2014
I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.
The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.
However, if I need to switch from vectors to something else then I am open to that as well.
The following example below compiles but crashes rather quickly because it is not thread safe.
How I can fix the example below which I can then apply to my larger project?
#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <algorithm>
enum EmployeeType {
[Code] ....
View 1 Replies
View Related
Apr 30, 2013
I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:
vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:
for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/
for (int b = 0; b < instance.structures[a].size(); b++){
for (int c = 0; c < instance.structures[a][b].size(); c++) {
if (1) { //checking value of variable not included in snippet
(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
}
}
}
}
The problem is in these two lines, I think:
(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?
View 4 Replies
View Related
Oct 26, 2014
i have this vector:
#ifndef new_thing_Inventory_h
#define new_thing_Inventory_h
#include <vector>
#include <string>
using namespace std;
class Inventory {
[code]....
so i know i need to use .push_back or .pop_back, but the program im using dosn't even recognize that inventory is a created vector.
View 6 Replies
View Related
Feb 9, 2015
How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.
here is my code:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)
[Code].....
View 3 Replies
View Related
Jun 7, 2012
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
View 9 Replies
View Related
May 13, 2013
I need to create a class vector as a template and define operations on vectors.
And this is what I made.
#include<iostream>
using namespace std;
template<class T>
[Code].....
View 2 Replies
View Related
Mar 26, 2013
Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>
How do I make a function that creates vector of vector of every different integers?
<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>
and so on...
View 2 Replies
View Related
Sep 26, 2014
I don't have in depth code or anything. I tried this but can't seem to wrap my head around it.
Code: //header.h
namespace test {
int arr[5];
[Code] ....
Also tried putting int arr[5] in a Test class within test.h.
I have 2 structs in another file, the main, and want to make an instance of the arr variable, in a separate header, for each.
View 2 Replies
View Related
Nov 21, 2013
How to i access setter from within a setter?
I know that getter methods can be access like
Code: getAccounts().getAccountNumber();
// for example but for setters how do i do it? this doesnt seem to work though
Code: setAccounts().setAccountNumber(accountNumber);
View 7 Replies
View Related
Feb 19, 2013
Here is the link for my program, I want to access the average value which is located in grade.c (calculate_grade) class through driver.c (main function) but I don't know how to make "average" visible
[URL] .....
View 1 Replies
View Related
Aug 30, 2013
I came across the below code snippet in a project and was not sure how value of variable "response" is computed. Here as we can see, pic_data holds two one dimensional arrays but "response" access both the single dimensional array as two dimensional array.
Code:
#define MAX 100
#define MAXBUF 100
u32 response;
u32 index;
}
[code]....
View 3 Replies
View Related
Aug 3, 2013
I've been attempting to create a game with curses and I keep running into the problem of scope. I want to change or use variables of a different class in a different header file. But I don't know if I should use pointers, references or neither. Should I be programming in a manner that doesn't make it necessary to use variables outside of their class?
View 1 Replies
View Related
Feb 8, 2014
I'm currently trying to access a variable contained within a base object from another completely different object and I continually get error messages. I'll attempt to put up the important code since it's contained within fairly large classes. From the Base .cpp file. ObjectPosition:
void ObjectPosition::init(float x,float y, float speed, int boundx, int boundy, float hit, int lives, bool live) {
ObjectPosition::x = x;
ObjectPosition::y = y;
ObjectPosition::speed = speed;
ObjectPosition::boundx = boundx;
ObjectPosition::boundy = boundy;
ObjectPosition::hit = hit;
ObjectPosition::lives = lives;
ObjectPosition::live = live;
}
This is the initialization function for the BaseObject. All objects are inheriting these variables which are Protected within the ObjectPosition class.Then they are initialized within the Pig class thus wise:
void Pig::binit(float sx,float sy, ALLEGRO_BITMAP *simage) {
//Sets all ObjectPosition Variables
ObjectPosition::init(800,900,10,80,40,40,10,true);
smaxFrame = 4;
scurFrame = 0;
[code]...
I tried to initialize the boundx through the pig via pinitx but I get errors and I can't access through the pig to the object position to the boundx.
View 10 Replies
View Related
Apr 4, 2013
I have a Visual C++ solution file that contains 3 projects. i want to access the variables declared in a function in a project from a function in another project. Function declarations are in .h file and expansion in .cpp files. Can i use friend class or any other suitable method.
View 6 Replies
View Related
Aug 31, 2014
How do i access the private array? I tried this and it's not working. Want i want to do is create class object in main and pass the string to constructor, and set m_make, m_model to that string. Then call the member function to output that.
const int BUFLEN = 256;
// class declaration
class CVehicle
{
[Code].....
View 2 Replies
View Related
Apr 4, 2014
I am trying to access the private member from main . The code is as follow. I wanted to call below function from main.
uint16_t Modbus::calcCRC(uint8_t u8length)
#define MAX_BUFFER 64
typedef struct {
[Code]....
View 1 Replies
View Related
Feb 9, 2014
I'm confused about accessing elements from arrays. Supposed I have an array of pointers to char:
char *names = { "John", "Rose", "Steven" };
To access one of the strings, should I use names[ 0 ][ i ], where i is an index in the set ( 0, 1, 2 ), or should I use names[ i ]? I would think it would be the first option, because this array has 1 dimension that contains others arrays, right?
View 8 Replies
View Related
Feb 11, 2013
I was wondering what the best way was to access data outside the function it was created in. I have the user input a data in a custom namespace function, and now im trying to access all those data points in a different function.
View 9 Replies
View Related
Jun 30, 2014
I need a "meaningful" way of accessing a table, the column is representing Err magnitude, and the row is representing Rate magnitude. For each error magnitude and rate magnitude, i define an action magnitude, which is the contains of the table. For example,
Code:
int matrix[10][10];
int Action1 = matrix[0][0];
int Action2 = matrix[0][1];
However, i need a better way of getting matrix[0][0], row and col itself is meaningless. I want to access the table like
"Action magnitude" = matrix["Rate magnitude 1"]["Err magnitude 2"]; using a string instead of int id.
How to easily do this in c++ container?
View 4 Replies
View Related
Apr 18, 2013
I need to translate a C program to C by making variables in structures private(no classes yet!) and putting public inline functions. There's a good chance that I have much more problems with my code than I'm asking right now, but I have 4 spots that I'm currently stuck in and can't access properly.
My structures:
Code: struct Container
{
private:
int count;
char** lines;
int nlines;
[Code] .....
View 4 Replies
View Related
Jul 26, 2014
I want to access the elements of my array dynamically. So far I've only figured out how to do this manually. if I tried it like this my code would work but there should be a better way right?
View 10 Replies
View Related
Oct 16, 2013
I've recently been learning GTK (though this question is not specific to GTK), and came across a situation that I was unsure how to best handle. Essentially, I've defined several pointers in one source file, and I want to access those pointers from other source files.
The structure of my GTK programs generally follow this pattern:
- "main.c": Define the main window and run GTK main
- "create_window.c": Create and arrange widget pointers in the main window
- "program_functions.c": All other source code for the project (several source files in reality)
In "create_window.c", I declare and define all my widget pointers (e.g. label). If I need to modify those widgets in "program_functions.c" for any reason (say, to change the value of a label), I need access to the pointers created in "create_window.c".
My first thought was to create a global struct of pointers in "create_window.c", and extern that struct to the other source files that need access to the pointers. The thing I don't like about this approach is spreading globals across my program.
My second idea was to create access functions in "create_window.c" where the necessary pointers are statically stored. The first time I call this function (immediately after creating a widget), a static copy of that pointer is stored in the function. Each time afterwards when I call that function (from other source files), I simply use that static pointer to access the widget of interest.
Example:
Code: void edit_label_1(GtkWidget *label_set, const char *string)
{
static GtkWidget *label = NULL;
[Code].....
Are either one of the approaches considered acceptable by standard practice?
View 2 Replies
View Related