Home > Back-end >  How to get item from list by giving index number?
How to get item from list by giving index number?

Time:05-28

I'm making program that will read text file and place each word in list named content and I have problem I don't know how to get words from that list in python it is 'content[index_number]'. thanks in advance!

//importing//
#include <iostream>
#include <sstream>
#include <list>
#include <fstream>


using namespace std;
using std::ifstream;

int main() 
{
std::string str = "";
std::list <string> content;

std::cout << "enter file name to read" << endl;
std::cin >> file;

//reading data//
ifstream indata;
std::string str;
indata.open(file);
indata >> str;
while (!indata.eof()) { indata >> str; content.push_back(str);}
indata.close();

return 0;
}

CodePudding user response:

Python lists are arrays and allow random access. So do the same in C and put your words in std::vector<std::string> content and you have content[i] just like in python, although content.at(i) is safer so use that.

  • Related