Hello,

As a part of my program, I have to convert a column of strings in a vector(containing strings of both upper and lower case) to uppercase. The column is accessed as array.at(i).at(2)
I have tried to convert character by character using toupper() , but I didnt work for me.
Please help me with this. I have my deadline tomorrow.

Thanks

Recommended Answers

All 8 Replies

you can use the transform function from <algorithms> header file

vector<string> lst;
for(int i = 0; i < lst.size(); ++i)
{
    transform(lst[i].begin(), lst[i].end(), lst[i].begin(), toupper);
}

Thankyou very much.

But I am accessing a two dimentional vector
I want to change the whole second column into upper case

array.at(x).at(2)

Can I use similar thing. What change I have to make for two dimentional array.

So you have a vector that looks like this ? vector< vector<string> > lst;

vector< vector<string> > lst;
    vector<string> a;
    a.push_back("hello");
    a.push_back("world");
    lst.push_back(a);
    for(size_t i = 0; i < a.size(); i++)
    {
        vector<string>& a = lst[i];
        for(size_t j = 0; j < a.size(); j++)
            transform(a[j].begin(), a[j].end(), a[j].begin(), toupper);
    }

sorry, there is no s on the end #include <algorithm>

I will try it. Thanks

Hi,

I didnt get any errors while compiling the same program you sent. But I got a message - vector subscrpit out of range. Did I make any mistake? Please help me.

This is the code I tried to run:

#pragma warning(disable: 4786) // VC++ 6.0 disable warning about debug line too long
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>

using namespace std;


typedef vector<string> a;



void main()
{
        vector< vector<string> > lst;
    vector<string> a;
    a.push_back("hello");
    a.push_back("world");

	
	lst.push_back(a);
    for(size_t i = 0; i < a.size(); i++)
    {
        vector<string>& a = lst[i];
        for(size_t j = 0; j < a.size(); j++)
            transform(a[j].begin(), a[j].end(), a[j].begin(), toupper);
    }

	//cout << lst.at(0).at(0);
return;
}

by the way I am using visual studio

Hi,

I didnt get any errors while compiling the same program you sent. But I got a message - vector subscrpit out of range. Did I make any mistake? Please help me.

Nope you didn't. But Ancient Dragon had a small error in his example. This line: for(size_t i = 0; i < a.size(); i++) Should have been: for(size_t i = 0; i < lst.size(); i++) Also read this about void main();

And is this you whole program? because you're including a few headers that you don't need...

When changing all the above:

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

using namespace std;

int main()
{
    vector< vector<string> > lst;
    vector<string> a;
    a.push_back("hello");
    a.push_back("world");
	
    lst.push_back(a);
    for(size_t i = 0; i < lst.size(); i++)
    {
        vector<string>& a = lst[i];
        for(size_t j = 0; j < a.size(); j++)
            transform(a[j].begin(), a[j].end(), a[j].begin(), toupper);
    }
    cout << lst.at(0).at(0);
    cout << lst.at(0).at(1);
    cin.get();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.