如何搜索矢量元素并替换它?

时间:2018-03-16 14:06:10

标签: c++ loops for-loop vector replace

#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");
}

如何创建一个搜索项目的循环“sugar”并将其替换为“honey”。 Sry,我是向量的新手

2 个答案:

答案 0 :(得分:5)

如果要替换字符串的第一个实例(如果存在),可以使用[Category("Data")] [Description("Indicates the source of data for the control.")] [RefreshProperties(RefreshProperties.Repaint)] [AttributeProvider(typeof(IListSource))] public object DataSource { get { return _ultraGrid.DataSource; } set { _ultraGrid.DataSource = value; } } [Category("Data")] [Description("Indicates a sub-list of the data source to show in the control.")] [Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", typeof(UITypeEditor))] public string DataMember { get { return _ultraGrid.DataMember; } set { _ultraGrid.DataMember = value; } } ,然后分配给返回的迭代器。

std::find

如果您想要替换所有实例

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";

答案 1 :(得分:0)

您可以使用标准算法std::find。例如

#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;
}

程序输出

Egg Milk Honey Chocolate Flour 

如果你想要替换所有出现的&#34; Sugar&#34;那么你可以使用标准算法std::replace

例如

#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;
}

程序输出

Egg Milk Honey Chocolate Flour Honey 

如果你的意思是只在循环中的函数print中进行替换,那么函数可以按以下方式看待

#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;
}

它的输出是

[0] Egg
[1] Milk
[2] Honey
[3] Chocolate
[4] Flour