如何在预定义的字符串中添加字符?

时间:2017-04-06 01:29:52

标签: c++

我创建了一个函数来分隔我的一个数据点以消除一个字符。我想将R:G:B:添加到3个数字中。因此,例如,如果值为255,0,0而不是

255
0
0

我希望它是

R:255
G:0
B:0

这是我用来分隔逗号的功能。

#include string
    void RGB(string input)
{
    istringstream ssString(input);

    while (getline(ssString, input, ','))
        cout<< input << endl;


}

2 个答案:

答案 0 :(得分:1)

您可以遍历一系列前缀。这样的事就足够了。

const char *prefix[3] = { "R:", "G:", "B:" };
for( int p = 0; p < 3 && getline(ssString, input, ','); p++ )
{
    cout << prefix[p] << input << endl;
}

答案 1 :(得分:0)

在不添加或减去代码中的行的情况下执行此操作的方法是:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
Map<String, Object> properties = new Hashtable<>();
properties.put("javax.persistence.schema-generation.database.action",
"none");
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect"); //you can change this if you have a different DB
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(adapter);
factory.setDataSource(this.springJpaDataSource());
factory.setPackagesToScan("package name");
factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
factory.setValidationMode(ValidationMode.NONE);
factory.setJpaPropertyMap(properties);
return factory;
}

三元运算符使用从-1开始的计数器变量,三元组消除了对几个if语句的需要。注意:你必须在括号@Bean public DataSource springJpaDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUrl("jdbc:mysql://localhost/SpringJpa"); dataSource.setUsername("tomcatUser"); dataSource.setPassword("password1234"); return dataSource; } 中包含三元组,否则编译器会向你抛出错误。