Code:
#include <iostream>
#include <vector>
using namespace std;
class A{
[Code]....
I read somewhere, that we can imagine the reference as a pointer to the vector. So, my question is:
Let's assume that instance of class A, named a, was created with new. We call a.getV() to foo and then we call the destructor of a. foo is safe? Is the copy constructor of std::vector called?
When returning an object by reference, only the address of the returned-object is returned, and that way we spare pushing a large object into the stack, and also spare time of pushing and popping large object to/from stack.
But what happens when the object that receiving the returned-object, is not a reference, but a 'regular' object?
How is the content of the returned object copied into the receiving object?
See for example in main, wid vs rwid. (I know in the case the returned-object is just one variable, there's no need to return it by reference, but its for simplifying the code).
class Rectangle { public: Rectangle(int w=0, int h=0);
I have this method that takes a pointer to a class object and right now when printing it, it's returning the location in memory 0x100300000. I've tried tweaking the function in a few different ways but I cant get it to return the object instead of the location.
Here's the vector that addSale accesses and the deceleration of the addSale ethod in the employee class.
#include<iostream> using namespace std; void getInput(int& numOfDays, double& itemPrice);
[Code].....
So this program is supposed to calculate the price of an item, and return it back as the variable total of type int. but for some reason it gives me a weird output on total. And am i using call by reference correct?
I am writing a function to take two vectors and put them end to end in a third vector. I'm new to working with vectors, and I cannot figure out why my append function is not returning vector C. I had the function print out vector C within it to make sure the logic in the function wasn't the problem, and it worked perfectly. My code is as follows:
#include <iostream> #include <vector> using namespace std; //append function to put vector b after vector a in vector c vector <int> append(vector <int> a, vector <int> B)/>/>/> { vector <int> c;
[code]....
and my output is as follows:
Vector A contains: 10 18 123 172 Vector B contains: 283 117 17
The two vectors back to back are:
Obviously, the third vector is not returning from the function to main properly, but why.
Suppose I have a stl vector of ints, and I want to pass a sub-range of that vector as an argument to a function. One way of doing that would be to copy the sub-range, and then pass that copy by reference, as follows:
Code: #include <vector> using namespace std; int MyFunction(vector<int> &a_vector) { // Do something return 0;
[Code] ....
However, it can be time-consuming to copy the elements between the two vectors if my_vector is large. So I'm wondering if it is possible to directly pass a sub-range of my_vector by reference, without having to create a new vector and manually copy over all of the relevant elements?
Okay, so for an assignment I need to write a function called find() that returns a reference to a vector. So I have vector <int> & find(string & key); If I do this, I get the obvious warning warning: reference to local variable 'lineNum' returned [enabled by default].
If I do vector<int> & find(string & key) const; I get a huge error that starts out like
In member function 'std::vector<int>& index_table::find(std::string&) const': indextable.cpp:74:30: error: no match for 'operator='
I asked a few questions a few weeks ago about vectors and the fact that their data is stored on the heap. When a function closes, anything in its scope is destroyed, if it's passed by reference it won't be destroyed since it's outside the scope.
I have a program where I create a vector in one function, then pass it by reference to another. When I test for memory leaks, I get told I have 1 memory leak in in my start() function, and one memory leak in my save() function.
It's just a simple program that creates a vector, populates it with some numbers, then saves the numbers in a file. If I'm passing my vector by reference to another function, do I need to manually do something to avoid memory leaks? I'll post the code below.
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)
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.
I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.
For instance, here is a test program that successfully finds the maximum according to just one member of the structure:
And the output was this, as expected: Maximum element S.a of vector<S> vec is at: 9 [I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6 [I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7 [I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8 [I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9
However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.
This was the description of the std::find function that I found: find - C++ Reference
Code: template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference
Code: template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:
Code: int x=7; std::vector<S>::iterator result; result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;
Is there another std function that I can use to make search and find an element according to just one member of myStruct?
Or perhaps there is a clever way to pass two arguments to the unary predicate.
I am making a game commonly know as the Hangman using C++.
Now I am trying to add a man in it like this:
0 |/ | /
Now the problem i am facing is that i am using a check that if a function returns the value 0 "return 0" it means the guess is wrong and it will not update the man but if it returns any value there will be a function called which will update the man.
I just wanna know that how i am going to use the check, the kind of thing that i am trying to use is, in general words "if(function returns a value) then update the man"
int main() { return match; }
How are we going to use it in check that if int main is returning 'match' in the check...
This simple little program is not returning a value. The output is
" (string) contains characters" (The number of characters is supposed to display between the 'contains' and 'characters.'
However, if I go to the function and cout the length, the cout in the main body displays just fine.
Here's the main portion :
cout << "'" << input << "' contains "; //Output of character count. charCount(input); cout << " characters, including any spaces. "; //Output of character count.
and here's the function.
int charCount(char *string) { int length = 0; //Variable to hold the number of characters. //Gets the number of characters contained in *string and puts that number into length.
How do I return a specific value from a function in order for me to cout it in my int main?
In this case i have solved for a value sigma1 in my function but i want to use the value of sigma1 in my int main also and in other places so how would i do that?
What the code below is not doing is returning a value for item to my variables (item1, item2, etc..) I'm not sure how I can make a function prototype that will ask for input and basically recycle after going through the program to a new value for each item. I also need to input individual tax for each item. I will also post it in this message:
As the title says, i'm using a function which returns a pointer to a struct:
the struct is the following:
Code: typedef struct POINT { uint16_t x; uint16_t y; }
Coordinate; the function i'm using:
Code: Coordinate * Read_XTP2046(void) {static Coordinate screen; //calculations to determine the coordinates screen.x=(temp[1]+temp[2])/2; screen.y=(temp[0]+temp[2])/2; // and so on... return &screen;}
The question is: how do i catch this pointer and make it into a Coordinate struct in which i can read the x and y.
In my main program i would do the following:
Code: Coordinate cor; cor = Read_XTP2046();
This does not work, as the function returns a pointer, but how to transform this pointer into a Coordinate struct.
I am currently doing a bodyfat calculator for a course in school.I'm having a problem getting an output returned from my while loop, which determines the user's gender by user input.My function (outside of main) is currently:
Code:
char gendercheck(gender){ while (true) { gender = _getch(); if (gender == 'M' || gender == 'm') { system("cls"); printf("Your selected gender is male."); break ;
[code]....
My failed tries include "return gender" and "return genderpicked" statements in both the if-functions and the loop itself, which I have deleted since that didn't work. My goal is to get a character stored in gender, so that I can use the information of the user's gender in my main function from this point and onwards.
I need to create a function which will print a list from 100Hz to 1000Hz then 1000Hz to 9000Hz. I have created a function in order to calculate and set up the frequency values from 100Hz to 9000Hz using two for loops as shown below. However I am unsure how to return this to the array at the main.
int main(void) { double Frequency[18]; system ("PAUSE"); return(0); } double Frequency (void) { int count;