Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to understand this line of code
vector<int>::iterator it = find(list_vector.begin(), list_vector.end(), 5)
where I have vector<int> list_vector; declared before hand.
What does the 5 do? What does it return? Does it return the 5 if it can find it at the beginning and end? If I wanted to make an if statement, and I wanted to find if the number 10 was in the statement (if it was, return true) how would I go about doing that?
vector<int>::iterator it = find(list_vector.begin(), list_vector.end(), 5)
std::find searches in the range defined by its first two arguments. It returns an iterator pointing to the first element that matches. If no element matches, it returns its 2nd parameter.
list_vector.begin() returns an iterator that points to the first element of list_vector.
list_vector.end() returns an iterator that points one element beyond the final element of list_vector.
5 is the target of the search. find() will look for an element that has the value 5.
If you'd like to determine if 10 is present anywhere in the vector, do this:
if(std::find(list_vector.begin(), list_vector.end(), 10) == list_vector.end())
std::cout << "No 10, bummer\n";
else
std::cout << "I found a 10!\n";
Or, if you'd like to simultaneously determine if 10 is present and determine its location:
std::vector<int>::iterator it = std::find(list_vector.begin(), list_vector.end(), 10);
if(it == list_vector.end())
std::cout << "No 10\n";
else
std::cout << "Look what I found: " << *it << "\n";
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
create a list:
list<int> l;
auto it = l.begin();
l.emplace(it, 0);
/*it =*/ l.emplace(it, 1);
/*it =*/ l.emplace(it, 2);
here to print
for (auto it : l)
cout << it << endl; // 0 1 2
cout << "\n\n";
cout << *it << endl; // = 3
Why is *it 3 and not 0?
At this line:
auto it = l.begin();
There is nothing in the list yet, so the beginning is also the end.
std::list::emplace() inserts items before the given iterator, so this line:
l.emplace(it, 0);
Places 0 before it, ie it inserts at the end of the list.
std::list also has some of the most forgiving iterator invalidation rules, and the it iterator is not invalidated as items are inserted into the list. it continues to refer to the end of the list as new items are added to the list.
Ultimately, this line:
cout << *it << endl;
Dereferences the "end" iterator, and this is not allowed. The results are undefined, and in this case it appears to contain the value 3. Maybe if you add another item then it'll appear to contain 4, and maybe you're seeing a hidden implementation-specific detail of the list. Or maybe the computer will crash or get a bad case of the nasal demons.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've scoured but not finding a solution; this is part of a homework assignment so looking more for tips/explanation than outright solution.
Problem:
I am parsing a file and extracting key elements into a map. I've declared my standard non-const map as : map<label,element>. In a second phase of the program, I am needing to locate if exists in "map" and replace its value.
I'm able to find the element, and print it, but I can't seem to get it to change. It's not a constant, so it should be editable, but maybe I'm using the wrong function?
((For reference, i is a line number (19, current value stored in map), value_i is a stored int variable I'm trying to insert into my second element (current value is 0) ))
for (auto &el : labels) {
if (el.second == i) {
el.second == value_i;
std::cout << "Label " << el.first << " value changed to: " << el.second << std::endl;
}
Output:
Label n value changed to: 19
Desired Ouput:
Label n value changed to: 0
Thanks in advance!!
you made a simple mistake which is el.second == value_i; - you didn't assign value for second, you checked if its equal value_i. If your compiler didn't give you any warning about it, I recommend setting a higher level of warnings ( you can read online on how to do it on probably every compiler), that way you won't miss such small mistakes.
If you change this line of code to:
el.second = value_i;
It will do what you desired.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
That is the code:
point [1][2][1] = 3;
cout << point[1][2][1] << endl;
point [1][3][0] = 4;
cout << point[1][2][1] << endl;
And that is what the console says when I run the application:
3
4
If I change to
point[1][3][0] = 5;
it says
3
5
How can I remove this annoying error? I cant continue that way.
When your variable is declared as
int point[100][100][1];
Then the valid indexes are respectively 0...99, 0...99, 0...0.
Your access to point[1][2][1] is therefore quite inappropriate. Depending on which index you make out of range, you might access an area outside the array entirely, or in a different slice of the array.
If you really want to access array elements arbitrarily, then I suggest you discard the triple-subscript notation and use:
int point[m][n][p];
int* p = &point[0][0][0];
p[x*n*p + y*p + z]
Now you are in control over row-major vs column-major access, and any computation that yields an offset less than m*n*p is valid.
Note that in your case m=n=100 and p=1, so that point[1][3][0] is p[1*100*1 + 3*1 + 0] = p[103] and also point[1][2][1] is p[1*100*1 + 2*1 + 1] = p[103]. So both really are the same location.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
int rVals[];
string rNum;
for (i=0; i < rNum.length(); ++i) {
if((rVals[i] < rVals[i+1]) && (rNum[i] =='C' || rNum[i]=='X' || rNum[i]=='I')){
continue; //checks to see if preceeding value is < the next value
} else {
valid = false;
cout << "you can't subtract by M, D, L, or V\n" << endl;
break;
}
}
rVals[] is a dynamic array and is set correctly. No matter what the input is the if statement seems to evaluate to false. what is wrong with the if statement?
Take a look at this: rVals[i] < rVals[i+1]. If rVals length is 10 for instance and i is 9 rVals[i+1] will "point" to the 11th element of the array (since the indexing of an array is starting from 0 and between 0 and 9 you heave 10 elements - the size of our array).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The program should be print the numbers 0 through 10, along with their values doubled and tripled. The data file contains the necessary variable declarations and output statements.
Example the output:
single 1 double 2 triple 3
single 2 double 4 triple 6
single 3 double 6 triple 9
single 4 double 8 triple 12
here my code tell me if correct
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int x, double, triple;
int x = 0
while (x<=10)
{
cout << "x" << double = (x*2) << triple = (x*3) << endl;
x++;
}
return EXIT_SUCCESS
I'll attempt to put a guiding answer so I'm not going to give you a straight code that you can just copy and paste into your homework, but if you read and follow it, it should be the answer. (And next time, do go find your lecturer, or your tutor).
Some issues:
You are not printing the "Single" and "double" and "Triple" text which you should (based on your expected answer) So add that in.
You did your calculation to get the number for double, and triple - good. But again, you did not print them out.
Also C++ allows you to stack multiple cout all on one line, so for example:
cout << "My name is " << myname << endl;
Will print out:
My name is (content of variable myname)
And then print an end of line (endl). You can use that same technique to do part of your assignment to print out the results to meet the expected output.
Credit to Synetech
you miss a lot of code in there. You didn't print nothing what you want
try again with this inside while loop:
cout << “single “ << x << double << x*x
<<“ triple “<< x*x*x << endl;
x++;