0
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;

void print(vector<string> v) {
   for (unsigned int i = 0; i < v.size(); i++) {
     cout << "[" << i << "] " << v[i] << "\n";
    }
 }

 int main(){
    vector<string> v(5);
    v[0] = "Egg";
    v[1] = "Milk";
    v[2] = "Sugar";
    v[3] = "Chocolate";
    v[4] = "Flour";

    print(v);

  system("pause");
}

How do I make a loop that searches for the item, "sugar" and replace it with "honey."? Sry, im new to vectors

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 7
    What have you tried? It looks like you know how to iterate through a vector and print it's elements and it is not to much different from that. – NathanOliver Mar 16 '18 at 14:07
  • I've tried with insert, but failed. – Недялко Ангелов Mar 16 '18 at 14:09
  • 1
    http://en.cppreference.com/w/cpp/algorithm/replace – Daniel Langr Mar 16 '18 at 14:09
  • 2
    A print function doesn't need a copy of the vector. Pass const reference. – llllllllll Mar 16 '18 at 14:11
  • I would suggest you either pick up a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list); or read the [documentation](http://en.cppreference.com/w/cpp/container/vector) for the classes you use more carefully. Insert is clearly documented to just add a new item. – UKMonkey Mar 16 '18 at 14:12
  • Possible duplicate of [How to replace specific values in a vector in C++?](https://stackoverflow.com/questions/9904976/how-to-replace-specific-values-in-a-vector-in-c) – p-a-o-l-o Mar 16 '18 at 14:56

2 Answers2

5

If you want to replace the first instance of the string (if it exists) you can use std::find then assign to the iterator that is returned.

std::vector<std::string> v {"Egg", "Milk", "Sugar", "Chocolate", "Flour"};

auto itMatch = std::find(v.begin(), v.end(), "Sugar");
if (itMatch != v.end())
    *itMatch = "Honey";

If you'd like to replace all instances

std::replace(v.begin(), v.end(), "Sugar", "Honey");
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You can use the standard alfgorithm std::find. For example

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main() 
{
    std::vector<std::string> v = 
    {
        "Egg", "Milk", "Sugar", "Chocolate", "Flour"
    };

    const char *src = "Sugar";
    const char *dsn = "Honey";

    auto it = std::find( v.begin(), v.end(), src );

    if ( it != v.end() ) *it = dsn;

    for ( const auto &s : v ) std::cout << s << ' ';
    std::cout << std::endl;

    return 0;
}

The program output is

Egg Milk Honey Chocolate Flour 

If you want to replace all occurences of "Sugar" then you can use the standard algorithm std::replace.

For example

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main() 
{
    std::vector<std::string> v = 
    {
        "Egg", "Milk", "Sugar", "Chocolate", "Flour", "Sugar"
    };

    const char *src = "Sugar";
    const char *dsn = "Honey";

    std::replace( v.begin(), v.end(), src, dsn );

    for ( const auto &s : v ) std::cout << s << ' ';
    std::cout << std::endl;

    return 0;
}

The program output is

Egg Milk Honey Chocolate Flour Honey 

If you mean the substitution only in the function print within the loop then the function can look the following way

#include <iostream>
#include <vector>
#include <string>

void print( const std::vector<std::string> &v, 
            const std::string &src = "Sugar", 
            const std::string &dsn = "Honey" ) 
{
    for ( std::vector<std::string>::size_type i = 0; i < v.size(); i++ )
    {
        std::cout << "[" << i << "] " << ( v[i] == src ? dsn : v[i] ) << "\n";
    }
}

int main() 
{
    std::vector<std::string> v = 
    {
        "Egg", "Milk", "Sugar", "Chocolate", "Flour"
    };

    print( v );

    return 0;
}

Its output is

[0] Egg
[1] Milk
[2] Honey
[3] Chocolate
[4] Flour
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335