Tags down


Assigning a Char Value to a Vector Element. C++

By : Nuzhat Shaikh
Date : October 17 2020, 06:10 AM
wish helps you You could try a std::map to create a mapping between a choice letter and an animal string. It makes it easier to query the map with a key for the associated value, here's an example:
code :
#include <iostream>
#include <map>

int main() {

    // making a mapping between two strings, a letter and animal
    std::map<std::string, std::string> pet = {{"a", "Snake"},
                                              {"b", "Bird"},
                                              {"c", "Fish"},
                                              {"d", "Turtle"}};
    // dialogue
    std::cout << "Select a pet: " << std::endl;
    std::cout << "(a) " << pet.at("a") << std::endl;
    std::cout << "(b) " << pet.at("b") << std::endl;
    std::cout << "(c) " << pet.at("c") << std::endl;
    std::cout << "(d) " << pet.at("d") << std::endl;

    // read input into a string variable
    std::string choice;
    std::cin >> choice;

    // output selected letter mapping to animal
    std::cout << "You selected a " << pet.at(choice) << std::endl;

    return 0;

Share : facebook icon twitter icon

Assigning vector::iterator to char array post VS 2003

By : Cai
Date : March 29 2020, 07:55 AM
To fix the issue you can do The proper solution would be to template FindNumMsgs such that it can work with either iterators or pointers (since pointers can be used as iterators just fine). Something like this:
code :
template <class T>
int FindNumMsgs(T it, int count) {
    while(count--) {
        // do whatever
    return n;

Determining most freq char element in a vector<char>?

By : flaimah
Date : March 29 2020, 07:55 AM
To fix the issue you can do If you are only using regular ascii characters, you can make the solution a bit faster - instead of using a map, use an array of size 256 and count the occurrences of the character with a given code 'x' in the array cell count[x]. This will remove an logarithm(256) from your solution and thus will make it a bit faster. I do not think much more can be done with respect to optimization of this algorithm.

push_back all contents in vector<char> to combine them as the first element of vector<string>

By : Christian S
Date : March 29 2020, 07:55 AM
this will help The problem is that you are trying to push a std::vector where a std::string is expected. So simply change the type of myBuffer to a std::string:
code :
#include <iostream>
#include <string>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;
    std::string myBuffer;

    for (int i = 0; i < mySentence.length(); i++) {
        if (mySentence[i] != ' ')
        else {

    if (!myBuffer.empty()) {

    // use myWords as needed...

    return 0;
#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;
    std::string myBuffer;

    std::istringstream iss(mySentence);
    while (iss >> myBuffer) {

    // use myWords as needed...

    return 0;
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;

    std::istringstream iss(mySentence);

    // use myWords as needed...

    return 0;

Could I pass an address of the first element of a vector of vector directly to a function which expects an unsigned char

By : farzyyy
Date : March 29 2020, 07:55 AM
hope this fix your issue Yes, you must convert them into one vector, for the first vector doesn't need to be continuous with the second vector.

Assigning to vector of char[8]

By : subhash Yadav
Date : September 29 2020, 08:00 PM
To fix the issue you can do You can'ṭ have a std::vector because char[8] does not satisfy the requirements of std::vector for the value type. In praticular, the value type is required to be Erasable, which char[8] is not given the default allocator.
You can use std::vector> instead. std::array is not only Erasable, but is also assignable.
Related Posts Related Posts :
  • Storing integers values from file into a vectors of vector
  • Why `static` functions in different TUs do not break the ODR?
  • Base class and templates
  • boost::asio allow non-blocking accept of new connections while handler for connection is blocking
  • How to write custom comparator for std::minmax function for datatype vector<glm::vec3> (opengl datatype)
  • Confused about * and & with pointers
  • What's the value in memory?
  • Qt - How to handle memory management for dialogs?
  • Why am I having trouble compiling a templated class?
  • Understanding which method will be invoked
  • Why does bool casting is called?
  • Insert string at linking time
  • How to use a C++ lambda to convert a member function pointer to a normal function pointer for use as a callback
  • Is the Intel C++ Compiler (19.0) now only using the Clang front-end (i.e. already abandoned EDG)?
  • Why does deleting the move constructor cause a compile error?
  • How can I make sure my random number between 0 and 1 generated by rand is not 0?
  • How will I pass ranges instead of iterator-pairs in C++20?
  • Use std::optional as a regular pointer vs use has_value() and value
  • How to interlace string with one character?
  • Derived class from Template argument doesn't have protected member access
  • Parallel For Loops: Find if a sorted array contains duplicate elements
  • Casting a reference to a base class: standard behaviour?
  • How to check dllmain function is returning false
  • How to update the attributes of an object?
  • How do I extract an integer in a string?
  • Why code give segmentation fault on inputs greater than 10?
  • Is it possible to access a Public Class Member from outside of the main() where it is created?
  • Why do we need .lib file in case of importing functions from .dll?
  • Why is ranges::ostream_iterator default-constructible?
  • Definition of a C++ variable, section Basic/6
  • How to add arithmetic operators to std::array?
  • Is it possible to to create a vector filled with zeros of size N using template meta programming in c++11
  • Problem about implementation of a stack in C++
  • Which of these pointer comparisons should a conforming compiler be able to optimize to "always false"?
  • Data Structure to represent command packet format
  • Can't find the error in my cpp or header file, class construction failing
  • How can I find substring in a vector in c++
  • Is it possible to pass std::deque's member functions as a parameter?
  • check if elements of a range can be moved?
  • How to pass a function as parameter in C++
  • What is the difference between _malloca and malloc?
  • is there an way to ignore signals signature?
  • Valgrind Invalid Read of Size 8 for insertion sort
  • C++ Template Specialization and Subclassing
  • C++ autoload default constructor on object property
  • Adding node to linked list without explicitly allocating memory space (without using new)
  • Why does "unsigned int" + "unsigned int" return an "unsigned int"?
  • Is there a reason why Clang does not optimize this code?
  • Returning a reference to a class data member and then trying to change that member
  • My code outputs invalid when I input anything
  • Why does a private struct defined in a .h require a scope in a return type in the .cpp file?
  • c++ wrapper for header encapsulation
  • Does a super class need a default constructor?
  • Generate random number and guaranteed to cover all between the given domain?
  • I'm trying to use something other than a boolean function
  • What is the principle for behavior of boost adding month?
  • Is there a way find a key in the whole json file using QJsonObject?
  • Why is my code printing the wrong ciphertext?
  • Forcing inlining of callback (lambda) in C++17 in library
  • How can I use Valgrind to determine the source of "still-available" memory leak?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk