Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
int main() { vector g1; vector :: iterator i; vector :: reverse_iterator ir;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end\t:\t";
for (i = g1.begin(); i != g1.end(); ++i)
cout << *i << '\t';
cout << endl << endl;
cout << "Output of rbegin and rend\t:\t";
for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << '\t' << *ir;
return 0;
}
Here in this code variable "i" has been declared as a iterator as well as a variable inside a for loop. isn't that a error?
If we see the first for loop it say that the loop will run till i!=g1.end() that means that the value of *(g1.end()) should not be displayed by *i but it is giving. ide shows output 1 2 3 4 5 for me it should be 1 2 3 4.
i is defined as an iterator in the argument list. When you redefine it in the first for loop, this is a new definition only for the scope of the loop -- this is perfectly legal, though not good practice.
vector::end() points to memory after the final item, not to the final item. So, yes, the final item in the vector will be printed.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I would like to create a vector that displays if there are equal numbers inside it if there are, an output will have to come out where it says that there are equal numbers if there are not the opposite,
EX: A(2,4,2,7) There are equal numbers
I'm trying any solution but can't figure out how to do it I'm at the beginning of the arrays.
Use std::set, fill it from your vector and compare if the sizes are equal. If not you have duplicates.
std::vector<int> values{1,1,2,3,4,5,5};
std::set<int> tmp{values.begin(),values.end()};
bool duplicates = (values.size() != tmp.size());
You could try to sort the vector first and then loop through the vector and check if current vector[i] == vector[i+1].
vector<int> yourVector{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
sort(yourVector.begin(), yourVector.end(), greater<int>());
int equalNum = 0;
for(int i = 0; i < yourVector.size()-1; i++)
{
if(yourVector[i] == yourVector[i+1])
{
equalNum++;
}
}
if(equalNum > 0)
{
std::cout << "There are: " << equalNum << "equal numbers" << std::endl;
}else{
std::cout << "There no equal numbers" << std::endl;
}
This is just some code i just made up haven't tested it.
Might be a good idea to use a map instead:
unordered_map<int, int> numbers;
++numbers[2];
++numbers[4];
++numbers[2];
++numbers[7];
Now you can check how many of each number you have:
cout << "Number of twos: " << numbers[2] << '\n';
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 8 months ago.
Improve this question
I am a beginner, and I am trying to learn C++.
For now, all I am trying to do is input 3 numbers, and print them back.
#include <iostream>
using namespace std;
int main(){
int n[2];
cout << "Enter three numbers" << endl;
for (int j = 0; j <= 2; j++){
cin >> n[j];
}
cout << "Debug " << n[2] << endl;
cout << endl;
for (int i = 0; i <= 2; i++){
cout << n[i] << "\t" << i << endl;
}
return 0;
}
Every time I print them, the last value of the array is modified, and I cannot figure out why! For a test input 6,7,8, the output is in the image below.
This for
for (int j=0;j<=2;j++){
cin>>n[j];
}
expects that the array has at least three elements with indices in the range [0, 2].
However you declared an array with two elements
int n[2];
If you are going to input three elements then the array should be defined as
int n[3];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
int main()
{
vector<int>numbers;
int numb = 0;
int i = 0;
int j = i - 1;
while (cin >> numb)
{
if (numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}
numbers.push_back(numb);
cout << "numbers[" << i << "] = " << numbers[i] << endl;
++i;
}
/*** I don't understand why a exception, run time error, break or whatever it names.....................................................
On the first iteration through the loop, j is -1. Accessing numbers[-1] is undefined behavior because the index is outside the bounds of the vector.
Indeed, accessing any index is out of bounds until you put something in the vector, so you cannot index numbers at all until you have called push_back on it at least once.
This code will display the message if the user enters a number already in the vector:
while (cin >> numb) {
vector<int>::iterator found = std::find(numbers.begin(), numbers.end(), numb);
if (found == numbers.end()) {
cout << "Numbers repeated" << endl;
}
// If you don't want duplicate numbers in the vector, move this statement
// into an "else" block on the last "if" block.
numbers.push_back(numb);
}
This code on the other hand will only display the message when a number was the same as the last number entered, that is, if sequential numbers are the same:
while (cin >> numb) {
if (!numbers.empty() && numb == numbers.back()) {
cout << "Numbers repeated" << endl;
}
numbers.push_back(numb);
}
You need to initialize your numbers vector with initial data, or check to make sure j is withing in vector size. A vector is essentially an array, and like any array, you cannot go out of bounds.
if (j < numbers.size() && numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to store this string into an an array.
space= 0
A,a =1
B, b =2
C , c = 3
.
.
Z, z= 26
string myArray[26] =
{ "A", "B", "C", "D",”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,
”Q”,”R”,”S”,T”,”U”,”V”,W”,X”,”Y”, ”z” };
for (int i = 0; i < myArray; i++)
{
myArray[] = myArray[i]
cerr << myArray[i] << endl << endl;
}
Is that how to get each character with number?
What you've got is an array of strings, not an array of characters. A string is a container of characters, in the sense that it is capable of holding multiple characters. Your task can be solved with one or two strings, depending on your design preferences (see below).
A, a =1
B, b =2
You are placing two characters per position. However, strings cannot hold more than one character at a single index. If you need both the upper and lower case character to occupy the same spot, you need to make either two strings, or two spots.
Here is the first approach (two strings):
string upper = " ABCDEF...";
string lower = " abcdef...";
int pos = ...; // The desired position
cout << upper[pos] << endl;
cout << lower[pos] << endl;
Here is the second approach (two positions):
string pairs = " AaBbCcDdEeFf...";
int pos = ...; // The desired position
cout << pairs[2*pos] << endl; // Upper
cout << pairs[2*pos+1] << endl; // Lower
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
as this is my first attempt at using pointers for vectors, I did some research on it and tried it on my program but however it doesn't give me the results I wanted,though it's closed to it.Please forgive me if I asking a lousy question.
Your error is when you print the contents of the vector, not when you add data to it.
for (int i= 0; i<5; i++) {
if(storeData.empty()) {
cout <<"<no other records available>" << endl;
}
// error: storeData[i] may not be valid
else if(storeData[i].getCivIndex() == 0 && storeData[i].getXOrdinate() == 0 && storeData[i].getYOrdinate() == 0) {
cout << "<no other records available>" << endl;
}
else {
cout << "Civ Index:"<< storeData[i].getCivIndex() << ",at Sector("<< storeData[i].getXOrdinate() << "," << storeData[i].getYOrdinate() <<")" << endl;
}
}
You're always looking to read 5 records here even if the vector contains fewer. You should check the size of the vector before dereferencing it, otherwise you are reading random memory and undefined behavior will result.
if(storeData.size() <= i) {
cout <<"<no other records available>" << endl;
}
if(storeData.size() > 5) {
storeData.resize(5);
}
first I think here you mean if(storeData.size()<5) otherwise the following printing will certainly fail since you only have access to storeData[0] while you try to access others.
When you do resize without a second argument, the items added into the vector will be default constructed. In case the PointTwoD struct doesn't provide a default constructor, its data members won't be initialized, which means it could end up with random values. That's why you have those weird numbers. (0 is also possible).
The solution is to define a default constructor for pointTwoD and set its data member to 0 in the initialization list or call resize with a second argument.