In C++ how can I insert multiple string in an array and print it, like:
Array[] = {"One", "Two", "Three", "Four".......};
I want to print them according to their index location.
Thank you.
If you want to use raw array, should be:
std::string strArr[] = { "One","Two","Three","Four" };
for (size_t i = 0; i < sizeof(strArr) / sizeof(std::string); i++)
{
std::cout << strArr[i] << " ";
}
std::cout << std::endl;
It's better to use vector instead of raw array:
std::vector<std::string> strVec = { "One","Two","Three","Four" };
for (size_t i = 0; i < strVec.size(); i++)
{
std::cout << strVec[i] << " ";
}
std::cout << std::endl;
Another way to traverse vector is to use iterator:
for (auto itr = strVec.begin(); itr != strVec.end(); itr++)
{
std::cout << *itr << " ";
}
std::cout << std::endl;
You can use standard vector to achieve your goal.
std::vector<std::string> stringVector = {"One","Two","Three","Four"}<br/>
std::vector can also be extended after its allocated.
Related
i would like to do a thing like this. can ou help me :) Thank you
/* Example: */
struct Name
{
const char *full_name;
const char *name;
};
std::vector<Name> n = { {"Harry Potter", "Harry"}, {"Hermione Granger", "Hermione"} };
// The expressions below does not work
string::const_iterator b = n.begin();
string::const_iterator e = n.end();
int s = n.size();
// ...
The problem mainly comes from the :
string::const_iterator b = n.begin(); // not possible
string::const_iterator e = n.end(); // not possible
int s = n.size(); // working as intended if you print it to the console
If you want to declare an iterator, in this case, you could do :
std::vector<Name>::const_iterator it;
If you would like to have control with the iterator, use it in a for loop :
for (it = n.begin(); it != n.end() - 1; it++) // Will stop at the first name in our example
{
std::cout << it->full_name << std::endl;
}
As it is pointing to n, in order to access its values, don't forget to use ->.
If you wanted to iterate through the whole vector, it is possible to use a simple loop.`
for (auto i: n)
{
std::cout << "My name is " << i.name << ", ";
std::cout << i.full_name << std::endl;
}`
On a side note, you did not need to declare "it" before it was used in the for loop.
The following would work too :
for (auto it = n.begin(); it != n.end() - 1; it++) // Will stop at the first name in our example
{
std::cout << it->full_name << std::endl;
}
For reference, here is my final code with examples, I hope that solves your problem :
struct Name
{
std::string full_name;
std::string name;
};
int main()
{
std::vector<Name> n = { {"Harry Potter", "Harry"}, {"Hermione Granger", "Hermione"} };
std::vector<Name>::const_iterator it;
for (auto i : n) // Printing the whole vector
{
std::cout << "My name is " << i.name << ", ";
std::cout << i.full_name << std::endl;
}
for (it = n.begin(); it != n.end() - 1; it++) // will return all the names except the last
{
std::cout << it->full_name << std::endl;
}
for (auto g = n.begin(); g != n.end() - 1; g++) // same as it, iterator initialized in the loop
{
std::cout << g->full_name << std::endl;
}
return 0;
}
I want to printout my vector from vector list
std::list < std::vector< unsigned int >> lista;
for (std::list< std::vector< unsigned int > >::iterator i = lista.begin(); i != lista.end(); ++i){
std::cout << std::hex << whats_here << " " << std::endl;
}
I don't know what should I place on whats_here. Any help will be appreciated.
Really simple:
for (auto &list_item : lista)
{
for (auto &item : list_item)
{
std::cout << std::hex << item << " " << std::endl;
}
std::cout << std::endl;
}
Make any required adjustment to get the desired output. Since the expected output is not given in the question, the above code is my best guess given your non working code.
I have a map which has two string as key and one vector as value
how can i print the value of map.
Below is my approach which is bad Can Someone help me thanks in advance
NOTE : i want to print by key not iterating on vector
int main()
{
vector<string>value;
std::map<std::pair<string,string> ,vector<string>> myMap;
string input1,input2,MyvectorValue;
for(int i=0;i<5;++i)
{
cin>>input1;
cin>>input2;
cin>>MyvectorValue;
myMap[std::make_pair(input1,input2)].push_back(MyvectorValue);
}
int j=0;
for( auto it = myMap.begin(); it != myMap.end(); ++it )
{
std::vector<std::string>& value = it->second.at(j++);
cout<<value // This is bad
//how can i print all map value ??
}
}
The value of the map is a vector, assuming you can use C++11, the following code would do what you need.
#include <string>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
int main()
{
std::vector< std::string >value;
std::map< std::pair<std::string , std::string> , std::vector<std::string> > myMap;
std::string input1,input2,MyvectorValue;
for(int i=0;i<5;++i)
{
std::cin>>input1;
std::cin>>input2;
std::cin>>MyvectorValue;
myMap[std::make_pair(input1,input2)].push_back(MyvectorValue);
}
//If you have a particular key (string1, string2), and want to print the values for that specific key...
auto particularKey = std::make_pair("stringA", "stringB");
for(auto val : myMap[particularKey])
std::cout << val << " ";
std::cout << std::endl;
// If you want to iterate through all keys of your map
for(auto &elem : myMap)
{
std::cout << "for the pair with key (" << elem.first.first << "," << elem.first.second << "), the value is the following vector" << std::endl;
for(auto s : elem.second)
{
std::cout << s << " ";
}
std::cout << std::endl << std::endl;
}
return 0;
}
You can print the keys by accessing the pair and then using first and second to get the first and second member of the pair respectively.
You can also print the values, by accessing the vectors and iterating over them, printing every string separately.
for(auto& element : myMap)
{
std::cout << "Key: {" << element.first.first << ", " << element.first.second << "}\n";
std::cout << "Value is a vector with the following strings: \n";
for(auto& str: element.second)
std::cout << str << std::endl;
}
If you want to print by key not iterating on vector , then you may declare map as "std::map,string> myMap". Then, you can do following modification to your code as given below.
int main() {
vector<string>value;
std::map<std::pair<string,string>,string> myMap;
string input1,input2,MyvectorValue;
for(int i=0; i<5; ++i) {
cin>>input1;
cin>>input2;
cin>>MyvectorValue;
myMap[std::make_pair(input1,input2)]+=MyvectorValue;
myMap[std::make_pair(input1,input2)]+= " ";
}
for( auto it = myMap.begin(); it != myMap.end(); ++it ) {
std::string& value = it->second;
cout<<value<<endl;
}
}
In my C++ script, I want to insert some elements of a list into a vector (from the beginning of list to a specific position "it"), and then try to add the vector at the top of the list and keeping the same order of the vector but I get unwanted additional elements in the vector.
Here is my code:
#include <iostream>
#include <iterator>
#include <vector>
#include <list>
int main() {
std::list<int> mylist;
for (int i = 0; i < 10; i++)
mylist.push_back(i * 10);
for (std::list<int>::iterator i = mylist.begin(); i <= mylist.end(); ++i) {
std::cout << *i << ", ";
}
std::cout << std::endl;
std::advance(it, 6);
std::cout << "The 6th element in mylist is: " << *it << std::endl;
// The vector that will contain mylist elements
std::vector<int> intAdeplacer;
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
std::cout << std::endl;
std::cout << "Nombre d'éléments dans le vecteur : " << intAdeplacer.size() << std::endl;
std::cout << std::endl;
// see the content of the vector
std::cout << "Le vecteur de deplacement contient : " << std::endl;
for (std::vector<int>::const_iterator i = intAdeplacer.begin();
i <= intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
I get this output:
Le vecteur de deplacement contient :
0, 10, 20, 30, 40, 134985,
134985 is not wanted..
// Insert in front of the list the values of the vector and keeping the same order of elements in the vector
for (std::vector<int>::const_iterator i = intAdeplacer.end();
i >= intAdeplacer.begin(); --i) {
mylist.push_front(*i);
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << "nouvelle composition de mylist : " << std::endl;
for (std::list<int>::const_iterator j = mylist.begin(); j != mylist.end();
++j) {
std::cout << *j << ", ";
}
std::cout << std::endl;
std::cout << std::endl;
// erasing the added elements from mylist
std::list<int>::iterator debut = mylist.begin();
std::list<int>::iterator fin = mylist.end();
std::advance(fin, 6);
mylist.erase(debut, fin);
for (std::list<int>::iterator j = mylist.begin(); j <= mylist.end(); ++j) {
std::cout << *j << ", ";
}
return 0;
}
If it is an iterator pointing at the 6th element of your list, the following insert() will insert from begin() (included) to it (excluded) into the vector:
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
So you'll have only 5 elements, from 0 to 40. Unfortunately your printing loop does include the end() of the vector, which is out of range. This is why you get this strange trailing number.
Just correct your loop into:
for (auto i = intAdeplacer.begin(); i != intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
Or consider this alternate range-for syntax:
for (auto &element: intAdeplacer) {
std::cout << element << ", ";
}
I have a vector array called nVectors.
vector<int>* nVectors[21];
for (int i = 1; i <= 20; i ++) {
nVectors[i] = generateVector(i);
}
I can print all the members of a single vector, but when it comes to the vector array, I still don't know how to print all the vectors in an array.
Maybe an iterator through all the member of a vector array and print using my predefined method pvector can solve this problem? But I don't know how to iterate in gdb.
std::array<std::vector<int>*, 21> nVectors;
for(std::array<std::vector<int>*>::iterator i = nVectors.begin();
i != nVectors.end();
++i)
{
for(std::vector<int>::iterator it = (*i)->begin();
it != (*i)->end();
++it)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;
Or, in C++11:
std::vector<int>* nVectors[21];
for(auto &i : nVectors)
{
for(auto &it : i)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;