Printing index of elements in a char vector
I have a program which writes a number of chars into a vector but I canĀ“t print an index of them. I need to know on which index where the chars are
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
// vector::operator[]
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//Function Declarations
void printIndex(const vector<char>& myVector);
int main ()
{
std::cout << "Give a person a name: " << endl;
//string inputString;
string inputString;
cin >> inputString;
std::string s(inputString);
std::vector<char> myVector( s.begin(), s.end() );
for ( char c : myVector ) std::cout << c << endl;
printIndex(myVector);
return 0;
}
//Function Definition
void printIndex(vector<char> myVector)
{
for (int i= 0; i < myVector; i++)
{
std::cout << i << "\n";
}
}
|
At line 37, you're attempting to use myVector as if it were a single number. Presumably, what you want to do is use the size of the vector?
Solved it! Thank you :)
1 2 3 4 5
|
for (int i= 0; i < myVector[i]; i++)
{
std::cout << i << "\n";
}
|
Topic archived. No new replies allowed.